simpledateformat

Why parsing '23:00 PM' with SimpleDateFormat("hh:mm aa") returns 11 a.m.?

Finally I managed to understand what the problem was. I should hold on my string 11 PM instead of 23 PM What I don't understand is why parsing '23:00 PM' with SimpleDateFormat("hh:mm aa") returns 11 a.m.? My guess is it adds 12 hrs to the parsed date. I hope there is not much problem. ...

Parsing standard date to GMT

Hey guys, Can someone show me a piece of java code that parses this date: 2009-08-05 INTO THIS GMT DATE: 2009/217:00:00 ==== what i have so far is: java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd"); java.util.Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); format.setCal...

Why does a new SimpleDateFormat object contain calendar with the wrong year?

I came upon a strange behavior that has left me curious and without a satisfactory explanation as yet. For simplicity, I've reduced the symptoms I've noticed to the following code: import java.text.SimpleDateFormat; import java.util.GregorianCalendar; public class CalendarTest { public static void main(String[] args) { Sys...

Inconsistent date parsing using SimpleDateFormat

I'm really scratching my head on this one. I've been using SimpleDateFormats with no troubles for a while, but now, using a SimpleDateFormat to parse dates is (only sometimes) just plain wrong. Specifically: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = sdf.parse("2009-08-19 12:00:00"); System.out.prin...

Java Date Format that allows - / or . as separators within date

What's the nicest way to parse a date that can be in one of the following formats "dd-MM-yyyy HH:mm" "dd/MM/yyyy HH:mm" "dd.MM.yyyy HH:mm" without creating 3 SimpleDateFormats and parsing against each one. Thanks ...

SimpleDateFormat and locale based format string

Hi, I'm trying to format a date in Java in different ways based on the given locale. For instance I want English users to see "Nov 1, 2009" (formatted by "MMM d, yyyy") and Norwegian users to see "1. nov. 2009" ("d. MMM. yyyy"). The month part works OK if I add the locale to the SimpleDateFormat constructor, but what about the rest? I...

Strange problem with timezone, calendar and SimpleDateFormat

Let's consider the following code: SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy", Locale.US); long start = sdf.parse("10:30:00 30/09/2009").getTime(); long end = sdf.parse("10:30:00 30/10/2009").getTime(); Calendar c = Calendar.getInstance(Locale.US); c.setTimeInMillis(start); System.out.println("Start = " + c.getTi...

What could cause this SimpleDateFormat formatting error?

I have a date stored as a java.sql.Timestamp in a database. The date is "2010-01-20T19:10:35.000Z" and is equivalent to 1264014635743 ms. Somehow, the date is formatted differently on the prod machine compared to the dev machine. The code to format the date is: private final static String DATE_FORMAT = "yyyy-MM-dd"; public final stati...

How to parse month full form string using DateFormat in Java?

I tried DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy"); Date d = fmt.parse("June 27, 2007"); Exception in thread "main" java.text.ParseException: Unparseable date: "June 27, 2007" The java docs say i should use four characters to match the full form. I'm only able to use MMM successfully with abbreviated months lik...

Java DateFormat and SimpleDateFormat returning a date that is incorrect

Today is Tuesday, February 9, 2010 and when I print the date I get the wrong date: SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date today = formatter.parse(String.format("%04d-%02d-%02d", Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInsta...

Groovy Sql and SimpleDateFormat help

In my database, I have a column type : datetime. Column data example : 2009-02-03 19:04:23.0 I'm using : SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); to define my date format. I'm using groovySQL to read tables and for each rows, I'm adding a new SampleJ object (mapped to a new table - SampleJ). Here my...

Date parsing/formating with TimeZone and SimpleDateFormat problem around DST switch

Hi everyone, I went throe multiple posts about TimeZone and SimpleDateFormat on Google and Stack Overflow, but still do not get what I'm doing wrong. I'm working on some legacy code, and there is a method parseDate, which gives wrong results. I attached sample JUnit which I'm trying to use do investigate issue. First method *(testPars...

SimpleDateFormat returns incorrect date on jdk1.4

String format = "yyyyMMdd"; SimpleDateFormat formatter = getSimpleDateFormat(format); formatter.setLenient(false); Date date = formatter.parse("07312011",new ParsePosition(0)); System.out.println(date); This gives "2500-01-01 00:00:00" on jdk1.4 which is incorrect and returns null on jdk1.5 Why does this give "2500-01-01 00:00:00" on...

Java SimpleDateFormat and compareTo

Hi, I've got a question about SimpleDateFormat class and the java.util.Date's compareto method: I'm constructing a Date object, then I format, finally I parse the formatted string and compare to the original date. DateFormat df = new SimpleDateFormat("yyyy.MMMdd hh:mm:ss SSS"); Date originalDate = new Date(); String s = df.format(or...

Why Java SimpleDateFormat().parse() is printing weird formate?

My input is String formated as the following: 3/4/2010 10:40:01 AM 3/4/2010 10:38:31 AM My code is: DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy hh:mm:ss aa"); try { Date today = dateFormat.parse(time); System.out.println("Date Time : " + today); } ...

simpledateformat parsing date with 'Z' literal

I am trying to parse a date that looks like this: 2010-04-05T17:16:00Z This is a valid date per http://www.ietf.org/rfc/rfc3339.txt. The 'Z' literal "imply that UTC is the preferred reference point for the specified time." If I try to parse it using SimpleDateFormat and this pattern: yyyy-MM-dd'T'HH:mm:ss It will be parsed as a...

how to parse a Date string to java.Date

Hi: I have a date string and I wang to parse it to normal date use the java Date API,the following is my code: public static void main(String[] args) { String date="2010-10-02T12:23:23Z"; String pattern="yyyy-MM-ddThh:mm:ssZ"; SimpleDateFormat sdf=new SimpleDateFormat(pattern); try { Date d=sdf.parse(date); ...

Why can't this SimpleDateFormat parse this date string?

The SimpleDateFormat: SimpleDateFormat pdf = new SimpleDateFormat("MM dd yyyy hh:mm:ss:SSSaa"); The exception thrown by pdf.parse("Mar 30 2010 5:27:40:140PM");: java.text.ParseException: Unparseable date: "Mar 30 2010 5:27:40:140PM" Any ideas? Edit: thanks for the fast answers. You were all correct, I just missed that one key se...

Getting pattern string from java SimpleDateFormat

I have a SimpleDateFormat object that I retrieve from some internationalization utilities. Parsing dates is all fine and good, but I would like to be able show a formatting hint to my users like "MM/dd/yyyy". Is there a way to get the formatting pattern from a SimpleDateFormat object? ...

How to insert sepcial characters in SimpleDateFormat?

hey guys i want to insert the word 'at' in the SimpleDateFormat so the date would be something like: Wed, 26 May 2010 at 11:17am i could able to make it without the at like: Wed, 26 May 2010 11:17am using SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy hh:mma"); so any ideas how to insert the word at to make it in t...