• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DateTimeFormatter

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys, I have a question about some code found on the mock exam 3 on http://sybextestbanks.wiley.com/
This question:
What does the code print

A 11:05
B 11:22
C 22:05
D 22:33
E the code doesn't compile
F a runtime exception is thrown

Now, I was fooled by the MM and answered B but the correct answer is A. Now, I do understand now that MM is referring to a month but shouldn't this throw an exception instead because the DateTimeFormatter isn't really properly formatted? I mean the dateTimeFormatter can have a date and a time all right, but this is effectively a mixture of the two, I didn't realize this was allowed.
thanks
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Now, I do understand now that MM is referring to a month but shouldn't this throw an exception instead because the DateTimeFormatter isn't really properly formatted?

With a DateTimeFormatter you can format the date and the time (hence its name). So why is "hh:MM" not a proper format according to you? Each format string will be a mixture of date and time symbols.
 
Narayana Bojja
Ranch Hand
Posts: 135
4
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean the dateTimeFormatter can have a date and a time all right, but this is effectively a mixture of the two, I didn't realize this was allowed. 


This is allowed. But In real projects , We don't format like this way. For exam, remember this perfectly valid way.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
You can use "weird" combinations of formatting. It's rare to actually want to, but legal.

One reason you might want a weird combination is if you were formatting for CSV. You could have columns year, minute, hour, day, month. Why? Legacy format maybe. The API doesn't want to prevent you from doing stuff that you might need like this.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. Sorry, it just felt really weird an my first reaction was "Oh this shouldn't compile" :-)!
One reason you might want a weird combination is if you were formatting for CSV. You could have columns year, minute, hour, day, month.
Thanks for the example Jeanne!
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Sorry, it just felt really weird an my first reaction was "Oh this shouldn't compile" :-)!

That's definitely a wrong first reaction! Once more, the compiler doesn't execute any code at all at compile-time. So it might have thrown an exception at runtime (e.g. "illegal format character"), but it doesn't as it's allowed to have a mixture of both date and time format characters. Because the ofPattern method accepts a single String parameter, it could never have been a compiler error. And you should be able to make this distinction ("runtime exception" vs "compiler error") on the actual exam.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic