• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting date from YYMM to DD-MM-YY  RSS feed

 
Paddy Row
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
How can I convert the date that's received in YYMM (ex: 2003) to DD-MM-YY(31-03-2020- this format has to be stored in my database).

Currently there is an existing piece of code  which is wrongly converting this date to 2020-01-01
In the below piece of code- format is yymm, dateString is 2003
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Padmavati Rowthula wrote:. . . YYMM (ex: 2003) to DD-MM-YY(31-03-2020-
That isn't DD-MM-YY, which would be 31-03-20.
this format has to be stored in my database). . . .
Dates in databases and Date objects are not stored with any particular format. The formats are only active when you view the dates.
Why are you copying the data from a field before you verify the argument?
Don't use Date or Calendar. They are awkward classes to use. Use the Java8 classes instead. You shou‍ld be able to work out a format for parsing the data, and use the last day of month method to create a new object set for the last day of the month. You shou‍ld be ablt to create an SQL date object from a LocalDate object.
 
Stephan van Hulst
Saloon Keeper
Posts: 7983
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you parse the date string to a Date object, then convert it to a timestamp, only to construct another Date object from it again?

Anyway, don't use Date. Use LocalDate instead, from the java.time API. After you parse the LocalDate from the date string, print it to see if the parsing code works correctly. That way you can check if the problem lies with parsing, or with storing in the database.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to understand the distinction between Date values and how they are printed on screen.

The value of a Date object and how it looks on screen (which format: YYMM, DD-MM-YY or whatever else) are two separate things.

A Date object does not know anything about formatting. It is only a holder for a date value.
This means that there is no way to make this work with a method as you have in your code, which returns a Date object.
There is simply no way to return a Date object that knows it should be printed as DD-MM-YY - a Date object simply is incapable of remembering how it should be printed.
So, it's impossible to implement your method, because it's based on the wrong assumption that the Date object knows what format it is in.

If you are storing the Date in a database, and the database column type is something like DATE, then it doesn't even matter.
A DATE column type is, just like a Java Date object, just a holder for a date value. It does not have a format by itself.
How it is displayed on screen (with which format), depends on the tool that you are using to display it. Not on the value in the database itself.

If you are using JDBC to store this in the database, you don't need to format your date at all. You'll need to convert your java.util.Date to a java.sql.Date object, and you can pass that
to your JDBC insert statement. No need to think about formatting at all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!