• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Time conversion question

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.text.SimpleDateFormat.

Just ignore the date part, and use only the time portion.
 
Tim Cerillo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));
 
Tim Cerillo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic