0

I have the following string "2015-04-02 11:52:00+02" and I need to parse it in Java to a Timestamp. I tried all sorts of formats including

SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+Z");

but nothing seems to work - I keep getting a ParseException

Can anyone help?

I need something like:

SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+Z");


Timestamp t = new Timestamp(mdyFormat.parse("2015-04-02 11:52:00+02").getTime());
Alexandr
  • 708
  • 9
  • 29
  • Post Your StackTrace !! – Neeraj Jain Apr 02 '15 at 14:18
  • Exception in thread "main" java.text.ParseException: Unparseable date: "2015-04-02 11:52:00+02" at java.text.DateFormat.parse(Unknown Source) at com.......Main.main(Main.java:45) – Alexandr Apr 02 '15 at 14:20
  • Not sure if you can use Java 8, but [this post](http://stackoverflow.com/questions/22463062/how-to-parse-format-dates-with-localdatetime-java-8) may help you. – Pete Apr 02 '15 at 14:38
  • If using [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html), be aware that the original version of java.time in Java 8 had a bug where it failed to parse offset-from-UTC values of just hours without minutes. I'm referring to the `+02` in the Question above, as opposed to hours:minutes value of `+02:00`. That hours-only value is indeed valid (defined by [ISO 8601](http://en.wikipedia.org/wiki/ISO_8601)) but does not parse with early versions of java.time. May be fixed in later updates to Java 8, though I've not tried. – Basil Bourque Apr 02 '15 at 18:43

4 Answers4

1

Try This

String str="2009-12-31 23:59:59 +0100";
                               /\
                               ||
                      Provide Space while providing timeZone

SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
System.out.println(mdyFormat.parse(str));

Output

Fri Jan 01 04:29:59 IST 2010
Community
  • 1
  • 1
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
0

try "yyyy-MM-dd HH:mm:ssX"

Z stand for timezone in the following format: +0800

X stand for timezone in the following format: +08

Examples here

jhamon
  • 3,603
  • 4
  • 26
  • 37
0

java.sql.Timestamp objects don't have time zones - they are instants in time, like java.util.Date

So try this:

SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp t = new Timestamp(mdyFormat.parse("2015-04-02 11:52:00").getTime());
Felipe SS
  • 79
  • 2
0

ISO 8601

Replace that SPACE in the middle with a T and you have a valid standard (ISO 8601) string format that can be parsed directly by either the Joda-Time library or the new java.time package built into Java 8 (inspired by Joda-Time). Search StackOverflow for hundreds of examples.

If using java.time, read my comment on Question about a bug when parsing hours-only offset value.

Example in Joda-Time 2.7.

String inputRaw = "2015-04-02 11:52:00+02";
String input = inputRaw.replace( " ", "T" );
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );  // Specify desired time zone adjustment.
DateTime dateTime = new DateTime( input, zone );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154