Manvender Singh
Manvender Singh

Reputation: 1

convert number dMMyy as date in java

I have date coming from database as type Number. I have to convert it into Date, like if I have date 070311 in db, then while exracting from db I am getting 70311 in java. 0 lost because it's number. So I convert this number into string and then format into date but not giving 07/03/11 as required.

String strDate = String.valueOf(70311);
Date date = new SimpleDateFormat("ddMMyy").parse(str);

output date I am getting Sat Sep 08 00:00:00 IST 3 which is 08/09/0003, but it suppose to be like 07/03/11.

Upvotes: 0

Views: 396

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339669

The Answer by nbrooks is good but uses outmoded classes.

LocalDate

Read his explanation, but use the modern java.time classes.

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

int inputNumber = 70311 ;
String inputModified = String.format( "%06d", inputNumber ) ;

DateTimeFormatter f = DateTimeFormatter.ofPattern( "ddMMuu" ) ;
LocalDate ld = LocalDate.parse( inputModified , f );

By the way, this idea of squeezing a date value into an integer number is terrible. When serializing date-time data, use the standard ISO 8601 formats. For a date this would be 2011-03-07 for March 7, 2011. These formats are used by default in the java.time classes when generating and parsing strings. And always use 4 digits for years as the ambiguity and difficulty in reading is not worth the spaced saved by two digits.


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, & java.text.SimpleDateFormat.

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

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

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

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: 1

nbrooks
nbrooks

Reputation: 18233

The dd in your format string means that there are two digits representing the value of days. In your string, you dropped the leading 0, so the day value is now only a single digit. The correct format string would therefore be dMMyy, which would give you the correct date.

The better solution would be to make sure you're not losing the leading 0 though, by not treating the date as an integer, or by pre-padding the number with leading zeroes.

Anyway, quick solution in this case would be this:

String strDate = "0" + String.valueOf(70311);
Date date = new SimpleDateFormat("ddMMyy").parse(strDate);

Or

String strDate = String.valueOf(70311);
Date date = new SimpleDateFormat("dMMyy").parse(strDate);

See the SimpleDateFormat documentation for more details.


Edit

A more reliable way of getting a String in the correct format (padded with 0s on the left side) is to use String.format, like this:

String.format("%06d", num);

As pointed out in the comments, this ensures that the 0 is added only in cases when it's needed:

String strDate = String.format("%06d", 70311);
Date date = new SimpleDateFormat("ddMMyy").parse(strDate);

Upvotes: 3

Related Questions