Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

AM vs PM problem with java.text.DateFormat  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When "2004-12-14 00:53:03.857" is parsed to "hh:mm:ss a" format using java.text.DateFormat, result is "12:53:03 AM" but it should be "12:53:03 PM". This happens only for any time between 12 - 13 hrs.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Vani Sreedharan:
When "2004-12-14 00:53:03.857" is parsed to "hh:mm:ss a" format using java.text.DateFormat, result is "12:53:03 AM" but it should be "12:53:03 PM". This happens only for any time between 12 - 13 hrs.


Surely "2004-12-14 00:53:03.857" is 53 minutes past midnight, so is actually 53 minutes into the start of the day, so therefore it should report AM and not PM ?? Sounds like its doing the right thing to me ???
 
Vani Sreedharan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also in case of 2004-12-14 12:53:03.857 its AM and not PM.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using an incorrect pattern for the String you are passing. See http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

hh is 12 hour time since you don't specify AM or PM in your String, SimpleDateFormat assumes AM.

Either add AM/PM to your String or use HH as your format which will make 00:53 AM and 12:53 PM
 
Vani Sreedharan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.text.ParseException;

public class test2
{

public static void main(String[] args)
{
String formattedTime = "";
String unformatedTime = "2004-12-14 12:53:03.857 pm";
try {

java.text.DateFormat dateTimeFormat =
new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.text.DateFormat timeFormat =
new java.text.SimpleDateFormat("hh:mm:ss a");
/*java.text.DateFormat timeFormat =
new java.text.SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");*/


if (unformatedTime != null && unformatedTime.trim().length() > 0) {
formattedTime =
timeFormat.format(dateTimeFormat.parse(unformatedTime));
System.out.println(formattedTime);
}

} catch (ParseException eParserException) {
System.out.println("In");
}
}

}


This was the program I tried. Please confirm if "timeFormat" is correct.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read Carl's response? Use "HH" and not "hh".
 
Vani Sreedharan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,that is not working out.
 
Vani Sreedharan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!... i got the problem solved.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!