Set date format and then convert it in Date from String- Java
Solution 1
You're not using the same format. Case matters! "MM" is for months, "mm" is for minutes.
Solution 2
The issue is you MM in the format.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
DateFormat formatter = new SimpleDateFormat("yyyy/mm/dd");
- MM is month
- mm is minute
Refer here for the detailed formats. So it should have been,
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Solution 3
+1 mael for spotting the format issue first, however...
Date
is a container for the number of milliseconds since the epoch (Jan-1970-01-01 GMT), it does not care about the format of the values.
For example, the following...
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date now = new Date();
String cur_date = dateFormat.format(now);
try {
System.out.println("Now = " + now);
System.out.println("cur_date = " + cur_date);
System.out.println("dateFormat.parse(cur_date) = " + dateFormat.parse(cur_date));
} catch (ParseException exp) {
exp.printStackTrace();
}
Outputs...
Now = Thu Aug 01 18:08:39 EST 2013
cur_date = 2013/08/01
dateFormat.parse(cur_date) = Thu Aug 01 00:00:00 EST 2013
So you can lose important data about the date by doing this...
Leave formatting to when you want to display the value, not when you want to store it (if you can get away with it)
Solution 4
It sounds like you're expecting a Date
to "know" its format. It doesn't. A Date
object just contains a number of milliseconds since the Unix epoch. It doesn't know about time zones, or calendars, or string formats. It's just a date.
So your code should just be:
myDb.setDOB(new Date());
... and then handle the formatting of that wherever you're displaying it.
Just_another_developer
Enthusiastic Java developer, Always keen to learn new things.
Updated on June 04, 2022Comments
-
Just_another_developer over 1 year
I am doing this to set a date format and then convert it into date datatype but it is not giving expected results.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); String cur_date=dateFormat.format(new Date()); myDb.setDOB(formatter.parse(cur_date));
Scenario is: I want current date to be converted in yyyy/mm/dd and then pass it in setDOB(Date date).
EDIT:Result added Result is Tue Jan 01 00:08:00 IST 2013
GUYS! I am not using 'mm',here I just mistakenly wrote mm in DateFormat it is also MM
EDIT AGAIN First I just used setDOB(new Date() ); but got formatting issue,
then I used SimpleDateFormat.format to set yyyy/MM/dd but it returns String so used
DateFormat.parse to convert it back into date type.
-
mael over 10 years+1 for this explanation. Formatting date + parsing the output can lose data.
-
Just_another_developer over 10 yearsI am doing exactly the same if u see the code I got new Date() but I wanted to format it as yyyy/MM/dd so I use simple formatter but it returns String so I have to use DateFormat.format to convert it back into date... what elso should I do?
-
Just_another_developer over 10 yearsI am not,I mistakenly wrote 'mm' here