Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Vani Sreedharan
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.
 
Nathan Russell
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.
 
Carl Trusiak
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
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!