How can I change the date format in Java?

2018-12-31 07:58发布

I need to change the date format using Java from

 dd/MM/yyyy  to yyyy/MM/dd

标签: java date
10条回答
查无此人
2楼-- · 2018-12-31 08:15
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
sdf.format(new Date());

This should do the trick

查看更多
君临天下
3楼-- · 2018-12-31 08:17

To Change the format of Date you have Require both format look below.

 String stringdate1 = "28/04/2010";  

 try {

     SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
     Date date1 = format1.parse()
     SimpleDateFormat format2 = new SimpleDateFormat("yyyy/MM/dd");

     String stringdate2 = format2.format(date1);
    } catch (ParseException e) {
   e.printStackTrace();
}

here stringdate2 have date format of yyyy/MM/dd. and it contain 2010/04/28.

查看更多
无色无味的生活
4楼-- · 2018-12-31 08:28

Or you could go the regex route:

String date = "10/07/2010";
String newDate = date.replaceAll("(\\d+)/(\\d+)/(\\d+)", "$3/$2/$1");
System.out.println(newDate);

It works both ways too. Of course this won't actually validate your date and will also work for strings like "21432/32423/52352". You can use "(\\d{2})/(\\d{2})/(\\d{4}" to be more exact in the number of digits in each group, but it will only work from dd/MM/yyyy to yyyy/MM/dd and not the other way around anymore (and still accepts invalid numbers in there like 45). And if you give it something invalid like "blabla" it will just return the same thing back.

查看更多
永恒的永恒
5楼-- · 2018-12-31 08:30
SimpleDateFormat newDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date myDate = newDateFormat.parse("28/12/2013");
newDateFormat.applyPattern("yyyy/MM/dd")
String myDateString = newDateFormat.format(myDate);

Now MyDate = 2013/12/28

查看更多
无与为乐者.
6楼-- · 2018-12-31 08:31

How to convert from one date format to another using SimpleDateFormat:

final String OLD_FORMAT = "dd/MM/yyyy";
final String NEW_FORMAT = "yyyy/MM/dd";

// August 12, 2010
String oldDateString = "12/08/2010";
String newDateString;

SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
查看更多
只若初见
7楼-- · 2018-12-31 08:34

Use SimpleDateFormat

    String DATE_FORMAT = "yyyy/MM/dd";
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    System.out.println("Formated Date " + sdf.format(date));

Complete Example:

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

public class JavaSimpleDateFormatExample {
    public static void main(String args[]) {
        // Create Date object.
        Date date = new Date();
        // Specify the desired date format
        String DATE_FORMAT = "yyyy/MM/dd";
        // Create object of SimpleDateFormat and pass the desired date format.
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        /*
         * Use format method of SimpleDateFormat class to format the date.
         */
        System.out.println("Today is " + sdf.format(date));
    }
}
查看更多
登录 后发表回答