How to change date format in Java?

I need to change the date format using Java from

dd/MM/yyyy to yyyy/MM/dd 
+49
java date
Aug 12 2018-10-12
source share
10 answers

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); 
+122
Aug 12 '10 at 16:13
source share
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); sdf.format(new Date()); 

That should do the trick

+24
Aug 12 '10 at 15:50
source share

Use SimpleDateFormat

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

Full 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)); } } 
+9
Aug 12 '10 at 15:49
source share

TL; dr

 LocalDate.parse( "23/01/2017" , DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK ) ).format( DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK ) ) 

2017/01/23

Avoid obsolete time classes

Christopher Parker's answer is correct but outdated. Inconvenient old time classes, such as java.util.Date , java.util.Calendar and java.text.SimpleTextFormat , are now deprecated , being superseded by java.time classes.

Using java.time

Parse the input string as a date object, then generate a new String object in the desired format.

The LocalDate class represents a date value only without time and without a time zone.

 DateTimeFormatter fIn = DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK ); // As a habit, specify the desired/expected locale, though in this case the locale is irrelevant. LocalDate ld = LocalDate.parse( "23/01/2017" , fIn ); 

Define a different formatter for output.

 DateTimeFormatter fOut = DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK ); String output = ld.format( fOut ); 

2017/01/23

By the way, consider using standard ISO 8601 formats for strings representing date and time values.




About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old obsolete time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises switching to the java.time classes.

To learn more, check out the Oracle tutorial . And search for qaru for many examples and explanations. The specification is JSR 310 .

Where can I get java.time classes?

  • Java SE 8 , Java SE 9 and later
    • Built in.
    • Part of the standard Java API with integrated implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the functionality of java.time is included back in Java 6 and 7 in ThreeTen-Backport .
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) specifically for Android.
    • See How to use ThreeTenABP ....

The ThreeTen-Extra project extends java.time with additional classes. This project is a proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and others .




Joda time

UPDATE: The Joda-Time project is now in maintenance mode , and the team advises switching to the java.time classes. This section is left here for the sake of history.

For fun, here is his code adapted to use the Joda-Time library.

 // Β© 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so. // import org.joda.time.*; // import org.joda.time.format.*; final String OLD_FORMAT = "dd/MM/yyyy"; final String NEW_FORMAT = "yyyy/MM/dd"; // August 12, 2010 String oldDateString = "12/08/2010"; String newDateString; DateTimeFormatter formatterOld = DateTimeFormat.forPattern(OLD_FORMAT); DateTimeFormatter formatterNew = DateTimeFormat.forPattern(NEW_FORMAT); LocalDate localDate = formatterOld.parseLocalDate( oldDateString ); newDateString = formatterNew.print( localDate ); 

Reset Console ...

 System.out.println( "localDate: " + localDate ); System.out.println( "newDateString: " + newDateString ); 

At startup ...

 localDate: 2010-08-12 newDateString: 2010/08/12 
+9
Dec 29 '13 at 3:13
source share
 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

+3
Dec 28 '13 at 15:02
source share

This is just Christopher Parker's answer , adapted to use the new classes 1 from Java 8:

 final DateTimeFormatter OLD_FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy"); final DateTimeFormatter NEW_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd"); String oldString = "26/07/2017"; LocalDate date = LocalDate.parse(oldString, OLD_FORMATTER); String newString = date.format(NEW_FORMATTER); 

1 well, not something new, Java 9 should be released soon.

+2
Jul 26 '17 at 15:44
source share

Or you can follow the regex path:

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

He works both ways. Of course, this will not confirm your date, and will also work for strings like "21432/32423/52352". You can use "(\\d{2})/(\\d{2})/(\\d{4}" to be more precise in the number of digits in each group, but it will only work with dd / MM / yyyy to yyyy / MM / dd, not vice versa (and still accepts invalid numbers in there like 45.) And if you give it something invalid, like "blabla", it will return the same thing back.

+1
Aug 12 '10 at 16:27
source share

many ways to change the date format

 private final String dateTimeFormatPattern = "yyyy/MM/dd"; private final Date now = new Date(); final DateFormat format = new SimpleDateFormat(dateTimeFormatPattern); final String nowString = format.format(now); final Instant instant = now.toInstant(); final DateTimeFormatter formatter = DateTimeFormatter.ofPattern( dateTimeFormatPattern).withZone(ZoneId.systemDefault()); final String formattedInstance = formatter.format(instant); /* Java 8 needed*/ LocalDate date = LocalDate.now(); String text = date.format(formatter); LocalDate parsedDate = LocalDate.parse(text, formatter); 
+1
Apr 14 '16 at 6:22
source share

To change the date format for which you want both formats to look lower.

  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 has the date format yyyy/MM/dd . and it contains 2010/04/28 .

0
May 4 '16 at 11:55
source share
 SimpleDateFormat format1 = new SimpleDateFormat("yyyy/MM/dd"); System.out.println(format1.format(date)); 
-one
Mar 27 '14 at 19:37
source share



All Articles