godin
godin

Reputation: 43

puzzled with how to convert ddmmyy string to dd-MMM-yy date

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.

Upvotes: 4

Views: 3794

Answers (6)

Basil Bourque
Basil Bourque

Reputation: 339659

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.

Upvotes: 2

Olaf
Olaf

Reputation: 6289

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

Upvotes: 0

raym0nd
raym0nd

Reputation: 3262

formatter = new SimpleDateFormat("dd-MMM-yy");
String s = formatter.format(date);

Upvotes: 1

emp
emp

Reputation: 5065

use SimpleDateFormat("dd-MMM-yy")

Upvotes: 1

RMT
RMT

Reputation: 7070

Why not use SimpleDateFormat?

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240948

Using SimpleDateFormat

Something like

DateFormat input = new SimpleDateFormat("ddMMyy");
DateFormat output = new SimpleDateFormat("dd-MMM-yy");
String result = output.format(input.parse(inputString));

Upvotes: 7

Related Questions