4

I am puzzled with how to convert ddmmyy string to dd-MMM-yy date in java code.Example--

041110 as a string will be 04-NOV-10 as date.

Any suggestion will be greatly appreciated.

Thanks.

RMT
  • 7,040
  • 4
  • 25
  • 37
godin
  • 43
  • 1
  • 4

6 Answers6

7

Using SimpleDateFormat

Something like

DateFormat input = new SimpleDateFormat("ddMMyy");
DateFormat output = new SimpleDateFormat("dd-MMM-yy");
String result = output.format(input.parse(inputString));
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    thanks, but if i provide 300201 or 300200 i get weired dates :( – godin Jul 20 '11 at 19:10
  • 1
    FYI, the 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 Apr 07 '18 at 21:00
  • @godin The modern classes used in [Basil Bourque’s answer](https://stackoverflow.com/a/49712040/5772882) with default settings will throw an exception when they get illegal dates like February 30. I agree with you that this is desired behaviour. – Ole V.V. Apr 08 '18 at 08:21
2

tl;dr

The other Answers using SimpleDateFormat are outmoded now. Use DateTimeFormatter instead.

LocalDate.parse(
    "041110" ,
    DateTimeFormatter.ofPattern( "ddMMuu" , Locale.US ) 
)
.format( 
    DateTimeFormatter.ofPattern( "dd-MMM-uu" , Locale.US )
)

04-Nov-10

java.time

The modern approach uses the java.time classes.

String input = "041110" ;

Define a formatting pattern to match your desired output. Note that we specify the Locale to determine the human language and cultural norms in generating the name of the month.

DateTimeFormatter fParse = DateTimeFormatter.ofPattern( "ddMMuu" , Locale.US ) ;

Parse the input to get a LocalDate object. The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = LocalDate.parse( input , fParse ) ;

Generate a String in standard ISO 8601 format.

ld.toString(): 2010-11-04

We have a LocalDate object in hand. We want to generate a String to represent that same value in another format.

DateTimeFormatter fGenerate = DateTimeFormatter.ofPattern( "dd-MMM-uu" , Locale.US ) ;

Generate the String object.

String output = ld.format( fGenerate ) ;

04-Nov-10

Tips

Generally best to let java.time automatically localize rather than hard-code a formatting pattern as seen here. Instead, use DateTimeFormatter.ofLocalized… methods.

Avoid the legacy date-time classes such as SimpleDateFormat. They are an awful mess of poor design. They were supplanted years ago by the java.time classes for many good reasons.

When exchanging data, never use custom formats such as that seen in the Question. Use the standard formats. They are designed to be unambiguous, easy to read by humans, and easy to parse by machine.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

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

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

Why not use SimpleDateFormat?

RMT
  • 7,040
  • 4
  • 25
  • 37
1

use SimpleDateFormat("dd-MMM-yy")

emp
  • 4,926
  • 2
  • 38
  • 50
1
formatter = new SimpleDateFormat("dd-MMM-yy");
String s = formatter.format(date);
raym0nd
  • 3,172
  • 7
  • 36
  • 73
0

Use SimpleDateFormat or have a map that translates numbers 1-12 to month name abbreviations.

Olaf
  • 6,249
  • 1
  • 19
  • 37
  • 2
    By the way… (a) Java 8 and later provides the [`Month`](https://docs.oracle.com/javase/10/docs/api/java/time/Month.html) enum, so no need to define your own. That class can localize month name. Ditto for `DayOfWeek`. (b) FYI, the troublesome old date-time classes such as `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 Apr 07 '18 at 21:18