0

I'm importing some data into some system and it asks me to provide a java date format string for the dates in my CSV document. I'm not a Java programmer, and I have difficulties of testing it. It asks to use syntax valid for SimpleDateFormat.

I have dates like this one Wednesday, January 2, 2013 5:29:26 PM +02:00 and this is the format string I came with based on documentation, however, it fails:

EEEE, MMM d, y hh:mm:ss a Z

I think the time zone format string is wrong, but I don't know the right solution. Please help.

Vladimir Prudnikov
  • 6,974
  • 4
  • 48
  • 57
  • The problem is `+02:00`, Java is expecting `+0200`. If you can correct this, it will parse correctly – MadProgrammer Apr 10 '13 at 00:30
  • You could also take a look at [this](http://stackoverflow.com/questions/2375222/java-simpledateformat-for-time-zone-with-a-colon-seperator) which discusses your problem and even [this](http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date) – MadProgrammer Apr 10 '13 at 00:34
  • Yeah, I can replace them in TextMate, timezone should be the same in all records since all data was written from the same place. Thanks, I'll try this. – Vladimir Prudnikov Apr 10 '13 at 00:47
  • @MadProgrammer please post your answer as an answer instead of the comment here, so, I'll be able to vote for it and mark as a correct answer. Thanks. – Vladimir Prudnikov Apr 16 '13 at 11:50

4 Answers4

2

The problem is +02:00, Java is expecting +0200. If you can correct this, it will parse correctly

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Consider Joda Time library, which has built in support for parsing and outputting ISO-format date strings.

Martin V.
  • 3,560
  • 6
  • 31
  • 47
  • Please read my question. I'm not a Java programmer and it is worthless to learn how to write and execute `Hello SimpleDateFormat` program, this should be very easy question for Java programmer. – Vladimir Prudnikov Apr 10 '13 at 00:16
  • Handling time looks like an easy task to most people but it usually turns out it isn't, i.e. many developers don't understand time zone offsets and universal time. – Cebence Apr 16 '13 at 21:46
0

To debug date parsing errors, print out the date format.

eg:

System.out.println(new SimpleDateFormat("EEEE, MMM d, y hh:mm:ss a Z").format(new Date()));

and then adjust until you see the right format.

hope that helps

Tom Carchrae
  • 6,398
  • 2
  • 37
  • 36
0

Hopefully, the answer to your problem:

C:\Users\Cebence\jdev\StackOverflow> java StackOverflow15914753
input = Wednesday, January 2, 2013 5:29:26 PM +02:00

Parsing input into date ............   [ OK ]

    date @ local = Wed Jan 02 16:29:26 CET 2013
ISO date @ local = 2013-01-02T16:29:26.000+01:00
        UTC date = 2013-01-02T15:29:26.000Z

This output was created by the following demo application. Note the importance of correct time zone, locale and the "X" instead of "Z" for parsing. The "X" handles "+02:00" and will work only on Java 7.

    // Important to be US!
    SimpleDateFormat format = new SimpleDateFormat(CUSTOM_FORMAT_WITH_TZ, Locale.US);
    // Important to be +2 hours offset!

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


/**
 * http://stackoverflow.com/questions/15914753/java-date-format-string-for-date-wednesday-january-2-2013-52926-pm-0200
 * input: "Wednesday, January 2, 2013 5:29:26 PM +02:00"
 * asking-for: working timestamp format string "EEEE, MMM d, y hh:mm:ss a Z"
 *
 * @author dejan.cebetarevic
 */
public class StackOverflow15914753 {
  private static final String ISO_FORMAT_WITH_TZ = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
  private static final String ISO_FORMAT_NO_TZ = "yyyy-MM-dd'T'HH:mm:ss.SSS";
  private static final String CUSTOM_FORMAT_WITH_TZ = "EEEE, MMMM d, y hh:mm:ss a XXX";
  private static final String DEFAULT_INPUT = "Wednesday, January 2, 2013 5:29:26 PM +02:00";

  public static void main(String[] args) throws Exception {
    // Assume default input.
    String input = DEFAULT_INPUT;

    // But if an input was given, use it.
    if (args.length == 1) {
      input = args[0];
    }
    System.out.println("input = " + input);

    System.out.print("\nParsing input into date ............");
    Date d = createCustomDateFormat().parse(input);
    System.out.println("   [ OK ]\n");

    System.out.println("    date @ local = " + d);
    System.out.println("ISO date @ local = " + isoFormat(d));
    System.out.println("        UTC date = " + utcFormat(d));
  }

  public static String utcFormat(Date date) throws Exception {
    SimpleDateFormat utc = new SimpleDateFormat(ISO_FORMAT_WITH_TZ, Locale.UK);
    utc.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
    return utc.format(date);
  }

  public static String isoFormat(Date date) throws Exception {
    SimpleDateFormat iso = new SimpleDateFormat(ISO_FORMAT_WITH_TZ);
    return iso.format(date);
  }

  public static SimpleDateFormat createCustomDateFormat() throws Exception {
    // Important to be US!
    SimpleDateFormat format = new SimpleDateFormat(CUSTOM_FORMAT_WITH_TZ, Locale.US);
    // Important to be +2 hours offset!
    format.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("GMT+2")));
    return format;
  }
}
Cebence
  • 2,406
  • 2
  • 19
  • 20