0

I have 3 messages, it message was sent in sequence, server give me 4 ISO8601 time:

2017-01-11T12:34:21.948631
2017-01-11T12:34:22.425915
2017-01-11T12:34:22.954749
2017-01-11T12:34:23.473965

My logic convert to current date

public class ISO8601{
    static SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSSSS");

    String isodate;
    long timestamp = 0;

    public ISO8601(String isodate){
        dateformat.setTimeZone(TimeZone.getTimeZone("UTC"));

        this.isodate = isodate;

        try {
            Date date = dateformat.parse(this.isodate);
            timestamp = date.getTime();


        } catch (ParseException e) {
            e.printStackTrace();

        }
    }

    public long getTime(){
        return timestamp;
    }

}

class for convert to date

public class HelperMethods {
  public static CharSequence getFormatTime(long time) {
        DateFormat sdf = new SimpleDateFormat("hh:mm:ss yyyy-MM-dd");
        Date netDat = new Date(time);
        return sdf.format(netDat);
    }
}

Now i try use this class and method for conver ISO8601 to normal time

Log.e("1", String.valueOf(HelperMethods.getFormatTime(new ISO8601("2017-01-11T12:34:21.948631").getTime())));
        Log.e("2", String.valueOf(HelperMethods.getFormatTime(new ISO8601("2017-01-11T12:34:22.425915").getTime())));
        Log.e("3", String.valueOf(HelperMethods.getFormatTime(new ISO8601("2017-01-11T12:34:22.954749").getTime())));
        Log.e("4", String.valueOf(HelperMethods.getFormatTime(new ISO8601("2017-01-11T12:34:23.473965").getTime())));

And it output:

01:50:09 2017-01-11
01:41:27 2017-01-11
01:50:16 2017-01-11
01:42:16 2017-01-11

If i will do * 1000L timestamp like this:

public static CharSequence getFormatTime(long time) {
        DateFormat sdf = new SimpleDateFormat("hh:mm:ss yyyy-MM-dd");
        Date netDat = new Date(time * 1000L);
        return sdf.format(time);
    }

It output:

09:00:31 48999-02-14
08:05:15 48999-02-08
10:59:09 48999-02-14
09:42:45 48999-02-09

I cant understand why i cant curectly convert date

g71132
  • 671
  • 2
  • 10
  • 17
  • see if this helps: http://stackoverflow.com/a/10621553/2074763 – Amey Shirke Jan 11 '17 at 12:42
  • Possible duplicate of [Convert iso8601 string date time format to date in Java](http://stackoverflow.com/questions/12201233/convert-iso8601-string-date-time-format-to-date-in-java) – W4R10CK Jan 11 '17 at 12:43
  • @Selvin thanks for answer. it turns out server give me not correct ISO8601 . Its format should look like `2017-01-11T12:34:21.948631+01:00` ? – g71132 Jan 11 '17 at 13:11
  • @Selvin i try did like this `new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");` but have same date like my qestion – g71132 Jan 11 '17 at 13:17
  • 1
    The class `SimpleDateFormat` is not appropriate here because your input is precise down to microseconds but your API is limited to milliseconds only so use ".SSS" at the end of your pattern (loosing the last 3 digits!). And don't use "h" but "H" for 24-hour-clock (as ISO-8601 mandates it). – Meno Hochschild Jan 11 '17 at 14:08
  • @MenoHochschild thanks for answer, i try did this line `new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");` but i have same time like my qestion `01:50:09 2017-01-11, 01:41:27 2017-01-11, 01:50:16 2017-01-11, 01:42:16 2017-01-11` – g71132 Jan 11 '17 at 14:31

1 Answers1

2

First thing to note:

SimpleDateFormat can only process milliseconds, not microseconds. So if you are willing to loose the trailing 3 digits of your ISO-8601-input then you could parse it with this pattern:

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

Note that this pattern uses "H" for 24-hour-clock, not "h" for 12-hour-clock. The hour 12 in input would otherwise become zero hour.

Next step: You seem to be willing to interprete the input in UTC by setting the timezone on your SimpleDateFormat-instance. It is clear that the input has neither a trailing "Z" (which must be interpreted as UTC+00:00 according to ISO) nor any kind of other offset information. That means: The interpretation as UTC is YOUR interpretation. In case of doubt please contact the papers/specs of the provider.

Then you want to format the parsed Date-instance to another format. But here you use again "h". Why? Specifying a 12-hour-clock without am/pm-marker does not make sense because the result is ambivalent. 01:00 would either be in the night or in the early afternoon if meant as 12-hour-clock.

I have already told you that a wrong symbol (here "h") would translate the hour 12 to zero hour. If you format such an hour IN YOUR DEFAULT TIMEZONE (your helper method for formatting does not set any explicit timezone) then an offset will be added to it. I think your system timezone uses one hour offset which explains the final formatted result where you get the hour 1.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126