• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date, SimpleDateFormat  RSS feed

 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am having a problem getting my head around the Date and SimpleDateFormat. I have tried several ways to get
get the date formatted in a the way I want, but I keep getting "Fri Apr 03 00:00:00 EST 1970" for my output.

My latest code is as follows:



Any help would be greatly appreciated..
Thanks!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're printing out a Date object - which has no notion of being formatted. What you want is the return value of the format method call (which the code currently ignores).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to format() does not change anything with the Date (which as Ulf, pointed out, has no formatting). Rather, format() returns the formatted sting, which your code is ignoring.

P.S. Your variables should not be in all uppercase as they are not constants.
 
Rehan Zahoor
Ranch Hand
Posts: 85
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DATE_OUT.format(bdate2); should be

bdate2 = DATE_OUT.format(bdate2);
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DATE_OUT.format(bdate2); should be bdate2 = DATE_OUT.format(bdate2);

No, format returns a String, not a Date.
 
W Akram
Greenhorn
Posts: 4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jd,

I messed around with the Date class a few weeks ago. I've attached my code below which is just trying things and doing nothing in particular. In the main what I really learnt is that you should no longer use Date class. I was going to investigate Joda Time library when I learnt Java 8 has a new java.time package and has the classes LocalDate, LocalTime and LocalDateTime when you need a complete time stamp. These are far easier and more intuitive to use. The Date class has a few quirks which can throw the unintitated and is also very user unfriendly. For example the int value for January is 0. So you can have a display value of e.g. 01-0-2014. Also although the class is called Date, really they should have called it Timestamp or Timepoint.



Here are the various ouputs from println statements:
Date is: Wed Sep 17 23:03:57 BST 2014
Date as long is: 1410991437960
Local Date is:2014-09-17
Local date plus 13 is 2014-09-30
Birthday is: 23.08.2014
Enter your birthday1 in format dd.mm.yyyy
01.01.2014
Enter your birthday2 - make less than bd1
01.06.2006
You entered bd1 01.01.2014
You entered bd2 01.06.2006
1388534400000 1149116400000 239418000000
Number of days between bd1 and bd2 is: 2771.0
dd Date is is: Wed Aug 03 02:00:00 BST 1977
1977-7-3 <-- August is printed as 07
1977-August-3
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a nice post

But never, never, never close a Scanner pointing to System.in.
 
Jd Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!