1
SimpleDateFormat format =new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
format.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
try {
    long diff2=format.parse(accidentReports.getAccidentDate());
}

getting this output :: Thu Dec 17 15:37:43 GMT+05:30 5

1615903
  • 32,635
  • 12
  • 70
  • 99
  • 1
    Your code doesn't print anything - where are you getting that output? – 1615903 Dec 17 '15 at 11:11
  • 1
    In which format u want the Date? – Raghavendra Dec 17 '15 at 11:14
  • 1
    What is setting `accidentReports.getAccidentDate()`? Your format string looks right to me, are you sure that the input is not setting the date to the year 5? – DaveyDaveDave Dec 17 '15 at 11:34
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Sep 04 '19 at 08:20
  • Possible duplicate of [how to parse output of new Date().toString()](https://stackoverflow.com/questions/4713825/how-to-parse-output-of-new-date-tostring) – Ole V.V. Sep 04 '19 at 08:22
  • It may also be a kind of duplicate of [Returning NumberFormatException form SimpleDateFormat in Java code](https://stackoverflow.com/questions/63500822/returning-numberformatexception-form-simpledateformat-in-java-code). – Ole V.V. Mar 28 '21 at 18:00

5 Answers5

10

If you required Date Object form String use:

    Date date=null;
    SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
    String temp = "Thu Dec 17 15:37:43 GMT+05:30 2015";
    try {
        date = formatter.parse(temp);
        Log.e("formated date ", date + "");
    } catch (ParseException e) {
        e.printStackTrace();
    }

Your OutPut is
Thu Dec 17 15:37:43 GMT+05:30 2015: in Date Object

For Again Convert it to String:

    String formateDate = new SimpleDateFormat("MM-dd-yyyy").format(date);
    Log.v("output date ",formateDate);

Now your output is:

12-17-2015
santoXme
  • 802
  • 5
  • 20
6

In Kotlin,

   private fun convertToCustomFormat(dateStr: String?): String {
        val utc = TimeZone.getTimeZone("UTC")
        val sourceFormat = SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy")
        val destFormat = SimpleDateFormat("dd-MMM-YYYY HH:mm aa")
        sourceFormat.timeZone = utc
        val convertedDate = sourceFormat.parse(dateStr)
        return destFormat.format(convertedDate)
    }

Input Date :

Sat Dec 19 12:50:57 GMT+05:30 2020

Output Date Format :

19-Dec-2020 12:50 PM

Explanation :

Format Explanation

  • EEE : Day ( Mon )
  • MMM : Month in words ( Dec )
  • MM : Day in Count ( 324 )
  • mm : Month ( 12 )
  • dd : Date ( 3 )
  • HH : Hours ( 12 )
  • mm : Minutes ( 50 )
  • ss : Seconds ( 34 )
  • yyyy: Year ( 2020 ) //both yyyy and YYYY are same
  • YYYY: Year ( 2020 )
  • zzz : GMT+05:30
  • aa : ( AM / PM )
Tippu Fisal Sheriff
  • 2,177
  • 11
  • 19
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table) - – Ole V.V. Dec 19 '20 at 18:36
  • - or see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Dec 19 '20 at 18:36
  • 1
    Also your `destFormat` format pattern is incorrect and will give you surprises around new year. You need to beware of the case of format pattern letters. *//both yyyy and YYYY are same* This is not true. – Ole V.V. Dec 19 '20 at 18:37
2

java.time

The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API* .

Using modern date-time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Thu Dec 17 15:37:43 GMT+05:30 2015";
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("E MMM d H:m:s O u", Locale.ENGLISH);
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);

        // Default string i.e. OffsetDateTime#toString
        System.out.println(odt);

        // A custom string
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssO", Locale.ENGLISH);
        String formatted = odt.format(dtfOutput);
        System.out.println(formatted);
    }
}

Output:

2015-12-17T15:37:43+05:30
2015-12-17T15:37:43GMT+5:30

Learn more about the modern date-time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Please check my solution i have used before:

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
    try {
        String temp = "Thu Dec 17 15:37:43 GMT+05:30 2015";
        Date expiry = formatter.parse(temp);
    } catch (Exception e) {
        e.printStackTrace();
    }
Androider
  • 3,833
  • 2
  • 14
  • 24
-1

Sorry this may be late to answer, but it may help to anyone in near future. just call this simple method while passing your date (Thu Dec 17 15:37:43 GMT+05:30 2015) and you will get result like this : 2015-12-17

 private String getFormattedDate(Date timeZone){

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String formattedDate = df.format(timeZone);
    return formattedDate;

}

Just keep in mind, this is a Date formatted value not a String value.

how to call :

 getFormattedDate(date);
Ramkesh Yadav
  • 987
  • 2
  • 11
  • 16