Forums Register Login

Time conversion question

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
java.text.SimpleDateFormat.

Just ignore the date part, and use only the time portion.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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));
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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));
+Pie Number of slices to send: Send
 

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.
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 955 times.
Similar Threads
back ground process id ??
date format with MS Access database
unable to retrieve timestamp in my servlet
unable to retrieve timestamp in my servlet
Date comparision in SQL from timestamp
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:43:24.