javan_novice
javan_novice

Reputation: 129

Going from MM/DD/YYYY to DD-MMM-YYYY in java

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

Upvotes: 12

Views: 146576

Answers (8)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

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.

Upvotes: 1

Jatin Devani
Jatin Devani

Reputation: 190

Try this

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); // Set your date format
String currentData = sdf.format(new Date());

Upvotes: 1

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 1125

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));

Upvotes: 3

Kanke
Kanke

Reputation: 2747

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate = LocalDate.now();
System.out.println("Formatted Date: " + formatter.format(localDate));

Java 8 LocalDate

Upvotes: 2

zod
zod

Reputation: 12437

formatter = new SimpleDateFormat("dd-MMM-yy");

Upvotes: -1

ASIK RAJA A
ASIK RAJA A

Reputation: 441

Try this,

Date currDate = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String strCurrDate = dateFormat.format(currDate);
System.out.println("strCurrDate->"+strCurrDate);

Upvotes: 2

CoolBeans
CoolBeans

Reputation: 20820

Below should work.

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date oldDate = df.parse(df.format(date)); //this date is your old date object

Upvotes: 0

Brian Clements
Brian Clements

Reputation: 3915

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

Upvotes: 28

Related Questions