1

I have a date format as "Nov 10,1980" in a string format(String str="Nov 10, 1980"), and i want to convert it to 1980-11-10. can any one tell me how to do that using java.

Thanks in advance

user972590
  • 251
  • 1
  • 5
  • 13

7 Answers7

2

You should first parse it from the original text format, then format the result using the format you want it to end up as. You can use SimpleDateFormat for this, or Joda Time (which is generally a much better date/time API).

Sample code using SimpleDateFormat:

import java.text.*;
import java.util.*;

public class Test {

    public static void main(String[] args) throws Exception {        
        String inputText = "Nov 10,1980";

        TimeZone utc = TimeZone.getTimeZone("UTC");

        // Or dd instead of d - it depends whether you'd use "Nov 08,1980"
        // or "Nov 8,1980" etc.
        SimpleDateFormat inputFormat = new SimpleDateFormat("MMM d,yyyy",
                                                            Locale.US);
        inputFormat.setTimeZone(utc);

        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd",
                                                             Locale.US);
        outputFormat.setTimeZone(utc);

        Date parsed = inputFormat.parse(inputText);
        String outputText = outputFormat.format(parsed);

        System.out.println(outputText); // 1980-11-10
    }
}

Note that:

  • I've explicitly specified the locale to use; otherwise if you try to parse the text on a system with (say) a French default locale, it will try to parse it using French month names.
  • I've explicitly set the time zone as UTC to avoid any daylight saving time issues (where a particular value could be ambiguous or even non-existent in the default time zone)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

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

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("MMM d,u", Locale.ENGLISH);
        LocalDate date = LocalDate.parse("Nov 10,1980", dtfInput);
        System.out.println(date);
    }
}

Output:

1980-11-10

ONLINE DEMO

Notice that I have not used a DateTimeFormatter to format the LocalDate because your desired format is the same as the ISO 8601 format which is also the standard used for java.time API. Check the LocalDate#toString documentation for more details.

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
1

Use this

            Date date = new Date();
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("yyyy-MM-DD");
    System.out.println(sdf.format(date));
vinay
  • 313
  • 1
  • 12
1
try {
    SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd, yyyy");
    Date strDt = sdf1.parse("Nov 10, 1980");
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(sdf2.format(strDt));
} catch (Exception e) {
    e.printStackTrace();
}
Vaandu
  • 4,857
  • 12
  • 49
  • 75
0
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String req_date = dateFormat.format(DATE)
System.out.println(req_date)
RanRag
  • 48,359
  • 38
  • 114
  • 167
0

You can use two SimpleDateFormats. One to parse, one to format. For example:

public static void main(String[] args) throws ParseException {
    DateFormat parseFormat = new SimpleDateFormat("MMM dd,yyyy");
    DateFormat displayFormat = new SimpleDateFormat("yyyy-MM-dd");

    Date date = parseFormat.parse("Nov 10,1980");
    String s = displayFormat.format(date);
    System.err.println(s);
}
ewan.chalmers
  • 16,145
  • 43
  • 60
0

Use the SimpleDateFormat to get the result you want

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89