File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes UTC formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "UTC formatting" Watch "UTC formatting" New topic
Author

UTC formatting

pria
Greenhorn

Joined: Jan 09, 2004
Posts: 5
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

Joined: Mar 04, 2004
Posts: 75
Have a look at java.text.DateFormat - this will let you manipulate date formats as much as you like.
pria
Greenhorn

Joined: Jan 09, 2004
Posts: 5
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

Joined: Jan 09, 2004
Posts: 5
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

Joined: Mar 04, 2004
Posts: 75
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

Joined: Jul 07, 2002
Posts: 10
'2004-03-09T23:11:35.205+00.00' ...how to get date in this format?
Sapna


Sapna
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UTC formatting