简体   繁体   中英

How to convert the date format

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

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)

Use this

            Date date = new Date();
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("yyyy-MM-DD");
    System.out.println(sdf.format(date));
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();
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String req_date = dateFormat.format(DATE)
System.out.println(req_date)

You can use two SimpleDateFormat s. 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);
}

使用SimpleDateFormat获得所需的结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM