2

I would like to convert this time stamps to Long value.

2016-07-13T21:11:45+00:00

I still don't know what the format of above time stamp, like MM/dd/yyyy hh:mm:ss aa to use with the SimpleDateFormat.

Son Nguyen Thanh
  • 1,199
  • 15
  • 19

4 Answers4

3

Try this code to

   String timeStr = "2016-11-01T09:45:00.000+02:00";
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
   Date dateObj= sdf.parse(timeStr);
   System.out.println(dateObj.getTime());
Android Surya
  • 544
  • 3
  • 17
1

It would be yyyy-MM-dd\'T\'HH:mm:ss and you can convert it to long by:

public static long convertDateToMilliseconds(String fromFormat, String sourceDate) {

        SimpleDateFormat sdfFrom = new SimpleDateFormat(fromFormat);
        //SimpleDateFormat sdfTo = new SimpleDateFormat(toFormat);

        Date date = sdfFrom.parse(sourceDate);
        //String convertedDate = sdfTo.format(date);

        return date.getTime();

}
Smit Davda
  • 638
  • 5
  • 15
0

Use this link to find in which formate to create Simple date formater then create date object with that date formater. Then you can get time in millis like below

long millisecond = beginupd.getTime();
Community
  • 1
  • 1
Prashanth Debbadwar
  • 1,047
  • 18
  • 33
0

SimpleDateFormat almost states this in its examples. For that you can use

"yyyy-MM-dd'T'HH:mm:ssXXX"
tynn
  • 38,113
  • 8
  • 108
  • 143
  • ```java.lang.IllegalArgumentException: Unknown pattern character 'X'``` – Son Nguyen Thanh Jul 20 '16 at 07:20
  • @SonNguyenThanh the documentation is really reliable sometimes. I found that people use `ZZZZZ` instead of `XXX` to read the timezones. Maybe this works like intended. – tynn Jul 20 '16 at 07:31