aspose file tools*
The moose likes Beginning Java and the fly likes Time conversion question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Time conversion question" Watch "Time conversion question" New topic
Author

Time conversion question

Tim Cerillo
Ranch Hand

Joined: Sep 10, 2002
Posts: 44
How can I convert a time format into another.

Ex.
String="4:00 am";
converted to this format
04:00:00-00:00

is there a class in java I can use?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

java.text.SimpleDateFormat.

Just ignore the date part, and use only the time portion.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Tim Cerillo
Ranch Hand

Joined: Sep 10, 2002
Posts: 44
Ok I did this:

DateFormat df = DateFormat.getDateInstance();
Date testDate = df.parse("4:00 am");

I got this error:
java.text.ParseException: Unparseable date:
[ May 19, 2005: Message edited by: Tim Cerillo ]
Thara Visu
Ranch Hand

Joined: May 17, 2005
Posts: 87
Try this
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

Date testDate = sdf.parse("4:00 pm");
DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL);
System.out.println(df.format(testDate));


Thara<br />SCJP 1.4 96%<br />SCBCD 1.3 96%
Tim Cerillo
Ranch Hand

Joined: Sep 10, 2002
Posts: 44
Originally posted by Thara Visveswaran:
Try this
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

Date testDate = sdf.parse("4:00 pm");
DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL);
System.out.println(df.format(testDate));


I tried this code and got this result:
4:00:00 AM EST
I also tried to use the pattern but I get errors
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ssZ");
It works when I use z instead of Z but I needed the whole TimeZone.
Thara Visu
Ranch Hand

Joined: May 17, 2005
Posts: 87
I think this is roundabout, but it gives u the complete Time Zone
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

Date testDate = sdf.parse("4:00 pm");

Calendar cal = Calendar.getInstance();


int offset = cal.getTimeZone().getRawOffset();

sdf.getCalendar().setTimeZone(new SimpleTimeZone(offset,""));
sdf.applyPattern("hh:mm:ss zzzz");

System.out.println(sdf.format(testDate));
Tim Cerillo
Ranch Hand

Joined: Sep 10, 2002
Posts: 44
Originally posted by Thara Visveswaran:
I think this is roundabout, but it gives u the complete Time Zone
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");

Date testDate = sdf.parse("4:00 pm");

Calendar cal = Calendar.getInstance();


int offset = cal.getTimeZone().getRawOffset();

sdf.getCalendar().setTimeZone(new SimpleTimeZone(offset,""));
sdf.applyPattern("hh:mm:ss zzzz");

System.out.println(sdf.format(testDate));


Almost...
I got this:
04:00:00GMT-00:00
How can I get rid of GMT?

Thanks for all your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Time conversion question