• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UTC formatting

 
pria
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to convert a date into UTC format.For example,
A date in this format '2004-03-09T23:11:35.205+00.00' needs to be converted to '2004-03-09T23:11:35.205Z' format.
Note here that the output needs to be a Calendar object in the above format.
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at java.text.DateFormat - this will let you manipulate date formats as much as you like.
 
pria
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried i t.It does not seem to work. This is what I tried
dateFormat=yyyy-MM-dd'T'hh.mm.ss.S'Z'
private java.util.Date formatDate(java.util.Date date)
{
java.util.Date toReturn = null;
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
formatter.setTimeZone(TimeZone.getTimeZone(timeZone));
//GregorianCalendar currentTime = new GregorianCalendar(TimeZone.getTimeZone(timeZone));
try
{
System.out.println("Date before Formatting"+date);
String strDate = formatter.format(date);
System.out.println("Date after Formatting1"+strDate);
toReturn = formatter.parse(strDate);
System.out.println("Date after Formatting"+toReturn);
}
catch(Exception e)
{
System.out.println("There is an exception in formatDate");
}
return toReturn;
}
 
pria
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry..forgot to add that this is the our put I get
Date before Formatting----Wed Mar 10 11:17:44 EST 2004
Date after Formatting1----2004-03-10T04.17.44.916Z
Date after Formatting----Tue Mar 09 23:17:44 EST 2004
I actually lost the formatting!!
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dates in Java are actually stored as some big number which reflects coordinates universal time, so formatting plays no part in how the date is stored i.e. it does not store it in a format like YYYY.MM.dd etc. What your code is showing is the effect of calling the default toString method of a Date object. If you want to *represent* it anyother way, use the DateFormat stuff. I can't understand a reason why you might want to change how Java stores its dates internally, perhaps you can explain?
[ March 10, 2004: Message edited by: john smith ]
 
Sapna Korecherla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'2004-03-09T23:11:35.205+00.00' ...how to get date in this format?
Sapna
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sapna,
Take a look at the class SimpleDateFormat. You'll do something like this:

Once you've set up the date format, using format() and parse() is straightforward. The format string above is *almost* what you want - I think it gets the timezone wrong. I haven't tested it, so I'm not sure. You might need to do something special to get a literal 'T' in there also.
Cheers,

--Tim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic