This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Time conversion question

 
Tim Cerillo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
Thara Visu
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic