0
        String startDate = "2018-07-29T09:50:49+05:30";


        String TAG = "Extra";
        final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

        DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMATE);
        try {
            Date date = df.parse(startDate);
            System.out.println(TAG + "Start: " + date.getTime());
            System.out.println(TAG + "Start: " + date.getDate());
            System.out.println(TAG + "Start: " + date.getHours() + ":" + date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

Its giving an error java.text.ParseException: Unparseable date: "2018-07-29T09:50:49+05:30"

Any idea what I am missing here?

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
  • https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date So probably you should use Joda-Time – Almas Abdrazak Jul 27 '18 at 05:46
  • You're missing the `.SSS` in your input, the milliseconds – logee Jul 27 '18 at 05:47
  • @AlmasAbdrazak The Joda Time library is made available in Java 8, under the package `java.util.time`. – MC Emperor Jul 27 '18 at 05:56
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). And as Yy said in an answer that isn’t an answer, definitely don’t use the deprecated `Date` methods `getDate` and `getHours`, they are not reliable. – Ole V.V. Jul 27 '18 at 08:41

4 Answers4

1

The new API turns out to be even easier in this case. Your pattern is the default format for java.time.ZonedDateTime:

ZonedDateTime date = ZonedDateTime.parse("2018-07-29T09:50:49+05:30")
ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • Great except it’s even better to use `OffsetDateTime` since the string has an offset and no time zone. – Ole V.V. Jul 27 '18 at 08:37
1

You can try something like this

       String time="2018-07-29T09:50:49+05:30";
       ZonedDateTime date = ZonedDateTime.parse(time);
       System.out.println(date);
       String TAG = "Extra";
       System.out.println(TAG + "Start: " + date.getDayOfMonth());
       System.out.println(TAG + "Start: " + date.toLocalDateTime());
       System.out.println(TAG + "Start: " + date.getHour() + ":" + date.getMinute())  ;              
Yash
  • 108
  • 1
  • 1
  • 7
  • Great except it’s even better to use `OffsetDateTime` since the string has an offset and no time zone. Rather than deleting your old answer and posting a new one you can just edit your answer. – Ole V.V. Jul 27 '18 at 08:44
0

You can use this method to get the date and time for your date: Below are the different formats of dates, you can use your own and pass it to the method as params.

    public  String localFormat = "yyyy-MM-dd HH:mm";
    public  String alarmFormat = "yyyy-MM-dd-HH-mm";
    public  String defaultFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    public  String calendarFormat = "yyyy-M-d";
    public  String calendarFormatCh = "yyyy-M-dd";
    public  String calendarFormatRc = "yyyy-MM-dd";
    public  String reminderFormat = "dd/MM/yyyy hh:mm a";

public  String getFormattedDate(Context mcontext, String date, String currFormat, String RequireFormat) {
        Utils.e(Tag + "750", currFormat + "date " + date);
        SimpleDateFormat sdf = new SimpleDateFormat(currFormat);
        SimpleDateFormat sdfReq = new SimpleDateFormat(RequireFormat);
        long time = 0;
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        try {
            time = sdf.parse(date).getTime();
            return sdfReq.format(time).toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

Just pass the date in the current format and in the format that you are expecting, it will return you accordingly. If you want time only, you can get using this method, you will need to implement it as per your requirement.

Ashu
  • 2,066
  • 3
  • 19
  • 33
-1

The Format you are using to parse has miliseconds too

 final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

You need to change that to

 final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ss.XXX";

I tried below example and it worked:

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


public class Test{

    public static void main(String[] args) {

            String startDate="2018-07-29T09:50:49+05:30";
            String TAG = "Extra";
            final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ssXXX" ;

            DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMATE);
            try {
                Date date = df.parse(startDate);
                System.out.println(TAG + "Start: " + date.getTime());
                System.out.println(TAG + "Start: " + date.getDate());
                System.out.println(TAG + "Start: " + date.getHours() + ":" + date.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }

    }
}

Output:

ExtraStart: 1532838049000
ExtraStart: 29
ExtraStart: 9:1532838049000
Ashu
  • 2,066
  • 3
  • 19
  • 33