Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

import calendar  RSS feed

 
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to input a date, and i want to find out what's the day of that day.
Format : mmddyyy

Example : 10/23/2015
How ?



Then .. ?
 
Sheriff
Posts: 4745
131
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on the version of Java you're on. If it's version 8, try looking at this:

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#of-int-int-int-

You would need to turn your Strings into integers.
 
Marshal
Posts: 58411
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If at all possible, only use the Java8 date classes (or something like Joda Time). They are much better than the older classes. What Knute showed you is an example; you can read a lot more about them in the Java™ Tutorials. you should be able to pass 23:10:15 and get 23rd October 2015. I expect the tutorial will tell you how.

Beware: if you use Scanner#nextLine() things can go horribly wrong if you have previously called its nextXXX methods. If anything nasty happens, start reading here.
 
Saloon Keeper
Posts: 4065
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using three digits for the year?
 
Campbell Ritchie
Marshal
Posts: 58411
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why did you write mmddyyy (where m does not mean month) in one place and 10/23/2015 with / / elsewhere?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Why are you using three digits for the year?



My mistakes. I meant yyy. Thanks
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Molayo Decker wrote:Try this



Why your program showing Day of January 1st 1970, not from my input or your example?
 
Gabrielle Linkherz
Greenhorn
Posts: 26
Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And why did you write mmddyyy (where m does not mean month) in one place and 10/23/2015 with / / elsewhere?



What do you mean? Well, if you're asking for my format, i did mean m : month, y : year, and so on. And when i used date format in excel, the format is month first. So, i think many people used this format, Don't they?
 
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gabrielle Linkherz wrote:Well, if you're asking for my format, i did mean m : month, y : year, and so on. And when i used date format in excel, the format is month first. So, i think many people used this format, Don't they?



Yes, a lot of people do that. And after they get strange results they post their question on a forum, where people tell them that "m" means "minute" and not "month" as far as DateFormat is concerned. It might also be a good idea for you to check the documentation for SimpleDateFormat to see what's the difference between "MM" and "MMM", and other useful things.
 
Campbell Ritchie
Marshal
Posts: 58411
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gabrielle Linkherz wrote:. . . i did mean m : month, y : year, and so on. And when i used date format in excel, the format is month first. . . .

Paul C has already told you that Java® will not interpret m as month. The month‑day‑year format is usually used in the United #states, but other formats are in common use in other countries.
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gabrielle,
Check the documentation on SimpleDateFormat http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html. Have you tried changing the date format to start from month to year?
 
Campbell Ritchie
Marshal
Posts: 58411
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better suggestion: stop using Calendar and its subclasses. Use LocalDate instead (Java8 only). More details in the Java™ Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!