0

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.

2 Answers2

3

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);
Tom
  • 16,842
  • 17
  • 45
  • 54
nbrooks
  • 18,126
  • 5
  • 54
  • 66
  • 1
    Adding zero in front is quite good solution but I would also suggest to check if string length has 6 digits. You should handle all marginal cases when you code. – deadfish Oct 15 '16 at 08:05
  • @deadfish I'm assuming that's a comment for the OP? I agree that the ultimate solution should handle any edge-cases, though any further improvements to this code are likely beyond the scope of this question/answer, and are left as an exercise for the reader. I'd argue though that an ideal solution to this problem would involve not reading the date from the database and storing it as an integer to be converted to a string and then interpreted as a date. p.s. `String.valueOf(70311)` will *always* be 5 digits, so checking the length would be pointless unless I had generalized this solution. – nbrooks Oct 15 '16 at 08:12
  • Yes, Your are right. The best solution would be store date in timestamp, then take it as long and format into any readable date. However my first comment was about Your first solution. Because if number of current day would be bigger than 9 then Your `strDate` would be like `0130311`. – deadfish Oct 15 '16 at 08:18
  • @deadfish I understood your comment, was just questioning how useful it would be to expand this example. Anyway, updated with a more general approach that would work for other dates as well. Thanks for the suggestion! – nbrooks Oct 15 '16 at 08:31
  • @Tom Thanks for the fix, didn't catch that somehow. – nbrooks Oct 15 '16 at 08:33
1

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.

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