The Artima Developer Community
Sponsored Link

Java Answers Forum
Timestamp format to yyyyMMdd using simple date format

1 reply on 1 page. Most recent reply: Oct 8, 2002 8:17 AM by Don Hill

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Rohit

Posts: 38
Nickname: rohit
Registered: May, 2002

Timestamp format to yyyyMMdd using simple date format Posted: Oct 8, 2002 4:37 AM
Reply to this message Reply
Advertisement
Hi All,

I m passing an string with date in timpestamp format,changing its format to yyyyMMdd,and adding one day in it,but my prob is that simple date format ,parses a different date value from timestamp string...here is the code
<code>
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

java.util.GregorianCalendar gCal = new GregorianCalendar();
gCal.setTime(sdf.parse(this.find("LastInterestAccrualDate").getValue()));
System.out.println("************" + this.find("LastInterestAccrualDate").getValue());
System.out.println("************" + sdf.parse(this.find("LastInterestAccrualDate").getValue()));

java.util.Date newDate = new java.util.Date(gCal.getTime().getTime() + 1000 * 60 * 60 * 24);

sdf = new SimpleDateFormat("yyyyMMdd");
System.out.println("********* " + sdf.format(newDate));

return sdf.format(newDate);
</code>


here is the output of above three println stmnts.
************2002-09-19 11:47:15.956000
************Sun Dec 09 00:00:00 GMT+05:30 2001
********* 20011210

so , in first line , the correct timestamp is being printed,but somehow , in second line,simpe date formats parse it to totally differnt value,and then add 1 day into it.

Any Idea?

thanks
Rohit


Don Hill

Posts: 70
Nickname: ssswdon
Registered: Jul, 2002

Re: Timestamp format to yyyyMMdd using simple date format Posted: Oct 8, 2002 8:17 AM
Reply to this message Reply
first off sdf.parse only creates a Date() from a string and if you don't call format() you won't get the formated date back, it appears to me that the
************2002-09-19 11:47:15.956000
is your value that you are parsing and
> ************Sun Dec 09 00:00:00 GMT+05:30 2001
is the default .toString() for a Date and
> ********* 20011210
is the correct format according to
SimpleDateFormat("yyyyMMdd");

So the question is what really do you want to happen ? If you want this format then here is how to do it.
----------------------
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
// learn to use the interface
java.util.Calendar gCal = new= new GregorianCalendar();

String strDate = this.find("LastInterestAccrual
Date").getValue();

Date newDate = sdf.parse( strDate);

String formatDate = sdf.format( newDate );

HTH



> Hi All,
>
> I m passing an string with date in timpestamp
> format,changing its format to yyyyMMdd,and adding
> one day in it,but my prob is that simple date format
> ,parses a different date value from timestamp
> string...here is the code
> <code>
> SimpleDateFormat sdf = new
> SimpleDateFormat("yyyyMMdd");
>
> java.util.GregorianCalendar gCal = new
> l = new GregorianCalendar();
>
>
>
>
>
>
>
> gCal.setTime(sdf.parse(this.find("LastInterestAccrual
> ate").getValue()));
> System.out.println("************" +
> ****" +
> this.find("LastInterestAccrualDate").getValue());
> System.out.println("************" +
> ****" +
> sdf.parse(this.find("LastInterestAccrualDate").getValu
> ()));
>
> java.util.Date newDate = new
> e = new java.util.Date(gCal.getTime().getTime() +
> 1000 * 60 * 60 * 24);
>
> sdf = new SimpleDateFormat("yyyyMMdd");
> System.out.println("********* " +
> *** " + sdf.format(newDate));
>
> return sdf.format(newDate);
> </code>
>
>
> here is the output of above three println stmnts.
> ************2002-09-19 11:47:15.956000
> ************Sun Dec 09 00:00:00 GMT+05:30 2001
> ********* 20011210
>
> so , in first line , the correct timestamp is being
> printed,but somehow , in second line,simpe date
> formats parse it to totally differnt value,and then
> add 1 day into it.
>
> Any Idea?
>
> thanks
> Rohit

Flat View: This topic has 1 reply on 1 page
Topic: no value in table... Previous Topic   Next Topic Topic: can any one answer me to these questions???

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use