12

Is there a method in Java that I can use to convert MM/DD/YYYY to DD-MMM-YYYY?

For example: 05/01/1999 to 01-MAY-99

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
javan_novice
  • 129
  • 1
  • 1
  • 3
  • 2
    Note that your pattern syntax is in fact invalid. It's `y` for years and `d` for day of month. The `Y` has no meaning and `D` is day of year. You want to convert from `MM/dd/yyyy` to `dd-MMM-yyyy`. – BalusC Nov 12 '10 at 22:33
  • You should have the Locale.English at the options:http://stackoverflow.com/a/2603676/2114308 – Phuong Jan 29 '16 at 04:42

8 Answers8

28

Use a SimpleDateFormat to parse the date and then print it out with a SimpleDateFormat withe the desired format.

Here's some code:

    SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
    SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
    Date date = format1.parse("05/01/1999");
    System.out.println(format2.format(date));

Output:

01-May-99
Brian Clements
  • 3,787
  • 1
  • 25
  • 26
  • 2
    You may want to supply a `Locale` to the SDF constructor, else it will just take the platform default locale for month names, which may not be the one you want to use. – BalusC Nov 12 '10 at 22:36
  • Very true, you can also set the time zone with `format1.setTimeZone(TimeZone.getTimeZone("GMT"));` – Brian Clements Nov 12 '10 at 22:38
  • 2
    For readers coming later to this question: The `SimpleDateFormat` class is long outdated and notoriously troublesome. Don’t use it. Since 2014 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`. See [the correct answer by Md. Asaduzzaman](https://stackoverflow.com/a/53246677/5772882). – Ole V.V. Nov 11 '18 at 10:06
3

java.time

You should use java.time classes with Java 8 and later. To use java.time, add:

import java.time.* ;

Below is an example, how you can format date.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
String date = "15-Oct-2018";
LocalDate localDate = LocalDate.parse(date, formatter);

System.out.println(localDate); 
System.out.println(formatter.format(localDate));
Md. Asaduzzaman
  • 799
  • 1
  • 9
  • 12
  • @Billal, If you add import java.time.*; in code block. then you should also add class name definition. I just want to clarify the developers which package I am using – Md. Asaduzzaman Nov 11 '18 at 08:35
  • Yes, that is very right, I actually forgot to roll back my edit, sorry, thank you – Billal Begueradj Nov 11 '18 at 08:37
  • I suggest always specifying explicitly the intended `Locale` needed for translating the name of the month. Here we would use something English related, such as `Locale.CANADA` or `Locale.US` or `Locale.UK`. Pass as the optional second argument to `ofPattern` method. The current default locale at runtime might not be English, in which case this code with this input would fail, throwing an exception. – Basil Bourque Nov 12 '18 at 20:17
  • 2
    For Java 6 & 7, must of the *java.time* functionality is available in the *ThreeTen-Backport* project. So no need to ever use the terrible `Date`/`Calendar`/`SimpleDateFormat` classes. – Basil Bourque Nov 12 '18 at 20:22
2

Try this,

Date currDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String strCurrDate = dateFormat.format(currDate);
System.out.println("strCurrDate->"+strCurrDate);
ASIK RAJA A
  • 431
  • 4
  • 10
  • 1
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Nov 12 '18 at 20:22
2
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.now();
System.out.println("Formatted Date: " + formatter.format(localDate));

Java 8 LocalDate

Kanke
  • 2,527
  • 16
  • 10
1

Try this

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // Set your date format
String currentData = sdf.format(new Date());
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jatin Devani
  • 190
  • 3
  • 15
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*.

A Date-Time parsing/formatting type is Locale-sensitive

A Date-Time parsing/formatting type (e.g. DateTimeFormatter) is Locale-sensitive i.e. the same letters will produce the text in different Locales .e.g. MMM is used for the three-letter abbreviation of month name and it can be different words in different Locales. In the absence of the Locale parameter, it will use the JVM's Locale. Therefore, never forget to use a Date-Time parsing/formatting type without the Locale parameter. Learn more about it from Never use SimpleDateFormat or DateTimeFormatter without a Locale.

You need two instances of DateTimeFormatter - one to parse the input string and another to format the output string, as per required patterns.

Demo:

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("MM/dd/uuuu", Locale.ENGLISH);
        String strDate = "05/01/1999";
        LocalDate date = LocalDate.parse(strDate, dtfInput);

        // The default string i.e. the value returned by LocalDate#toString
        System.out.println(date);

        DateTimeFormatter dtfOutputEng = DateTimeFormatter.ofPattern("dd-MMM-uuuu", Locale.ENGLISH);
        String formattedEng = dtfOutputEng.format(date);
        System.out.println(formattedEng);

        DateTimeFormatter dtfOutputFr = DateTimeFormatter.ofPattern("dd-MMM-uuuu", Locale.FRENCH);
        String formattedFr = dtfOutputFr.format(date);
        System.out.println(formattedFr);
    }
}

Output:

1999-05-01
01-May-1999
01-mai-1999

ONLINE DEMO

Some other important notes:

  1. Instead of Y (week-based-year), you need to use y (year-of-era) and instead of D (day-of-year), you need to use d (day-of-month). Check the documentation to learn more about it.
  2. Here, you can use y instead of u but I prefer u to y.

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

Below should work.

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date oldDate = df.parse(df.format(date)); //this date is your old date object
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
-1
formatter = new SimpleDateFormat("dd-MMM-yy");
Yogurt
  • 2,913
  • 2
  • 32
  • 63
zod
  • 12,092
  • 24
  • 70
  • 106