1

I'm trying to parse a timestamp with timezone string in Java. My strings are:

"2013-01-01 15:30:00.2 +05:00"
"2003-01-01 02:59:04.123 -8:00"

This is my code to parse it:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
simpleDateFormat.parse("2013-01-01 15:30:00.2 +05:00");

However, I'm getting this error message when I run the code:

java.text.ParseException: Unparseable date: "2013-01-01 15:30:00.2 +05:00"

I also tried using the following code to parse it:

DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTimeNoMillis();
DateTime dateTime = dateTimeFormatter.parseDateTime("2003-01-01 02:59:04.123 -8:00");
Timestamp timeStamp = new Timestamp(dateTime.getMillis());

This gives me exception:

Invalid format: "2003-01-01 02:59:04.123 -8:00" is malformed at " 02:59:04.123 -8:00"

I also tried inserting 'T' in the string after date but it also gave the invalid format exception:

 Invalid format: "2003-01-01T02:59:04.123 -8:00" is malformed at " .123 -8:00"

This must be really simple -- I don't know where I'm going wrong though.

Thanks for the help!

ajain
  • 488
  • 4
  • 23
  • this might help. http://stackoverflow.com/questions/4542679/java-time-zone-when-parsing-dateformat – Deendayal Garg May 03 '16 at 06:23
  • 2
    Note the "no millis" part of `ISODateTimeFormat.dateTimeNoMillis` - and then look at the value you're trying to parse. – Jon Skeet May 03 '16 at 06:26
  • 1
    I assume the `DateTimeFormatter` code is Joda Time, not Java 8? It would be useful if you'd indicate that in your post. – Jon Skeet May 03 '16 at 06:29
  • 1
    Is there any reason why you've got a leading 0 in +05:00 but not in -8:00? If you have any control over this format, it would really help if you could make it a lot more consistent - ideally just following ISO-8601. – Jon Skeet May 03 '16 at 06:32
  • @JonSkeet ya I see my mistake. I changed the format to: `DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();' But I am still getting the exception: `Invalid format: "2003-01-01T02:59:04.123-8:00" is malformed at "8:00"`. Also I want to keep the timestamp as is, I don't want to insert T in the string. – ajain May 03 '16 at 06:56
  • 1
    Well basically, you've got problems: "-8:00" doesn't match the expected time zone format of either `z` or `Z`. You don't need to put a `T` in - you can easily specify a custom format that will cope with that - but your life would be much easier if you could use `-08:00` instead of `-8:00`. – Jon Skeet May 03 '16 at 06:57
  • @JonSkeet I don't have control over the leading 0. It may be there or it may not. I am getting the timestamp from one database and have to insert it in another database table without any change using Java code. – ajain May 03 '16 at 07:00
  • If this is in a database, why are you getting it as text to start with? Is it being stored as text in the database? If so, that's horrible :( – Jon Skeet May 03 '16 at 07:01
  • @JonSkeet I am getting this from a CSV file. – ajain May 03 '16 at 07:03
  • 1
    I thought you said you were getting it from a database? Fundamentally, I would strongly urge you to try to fix the unruly formatting as close to its source as possible. – Jon Skeet May 03 '16 at 07:04

2 Answers2

0

You just make a mistake in SimpleDateFormat(yyyy-MM-dd HH:mm:ss.SSS z), Where 'z' accepts TimeZone with General time zone presentation.General time zone accepts :

GMTOffsetTimeZone:

GMT Sign Hours : Minutes
Sign: one of
         + -
 Hours:
         Digit
         Digit Digit
 Minutes:
         Digit Digit
 Digit: one of
         0 1 2 3 4 5 6 7 8 9

But you mentioned +5:00 which should be GMT+5:00.

There are also 'Z' and 'X' to represent TimeZone in SimpleDateFormat which accepts RFC 822 time zone and ISO 8601 time zone respectively.

Hailey
  • 157
  • 1
  • 18
0

Try the below code: Just put GMT+5:30 instead of +5:30

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDemo {

    // main method
    public static void main(String[] args) {

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
            Date d = simpleDateFormat.parse("2013-01-01 15:30:00.2 GMT+05:00");
            System.out.println(d);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
mattyman
  • 351
  • 2
  • 16