This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
Closed 6 years ago.
The code I've got should convert the date of birth input to DD/MM/YYYY format, which it does but for example when I input 20/08/2000 it sees the date as 3rd January.
System.out.println(this.dob);
DateFormat dateF = new SimpleDateFormat("dd/MM/YYYY");
Date birth = dateF.parse(this.dob);
System.out.println(birth);
Which outputs
20/08/2000
Mon Jan 03 00:00:00 GMT 2000
Using capital Ys in your format means something called the "week year".
Instead, use lowercase ys in your format, which means the year as you'd expect.
new SimpleDateFormat("dd/MM/yyyy");
Related
This question already has answers here:
How to properly format the date?
(5 answers)
Get Date type object in format in java
(6 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I have looked around for help on this, but again, it's just one of those things that I cannot find a suitable answer to my specific issue.
Here's 2 very detailed (and helpful) SO posts that I've looked at:
Change date format in a Java string
Java string to date conversion
This is what I have:
//Date Formatter
SimpleDateFormat dateFormatter1 = new SimpleDateFormat("yyyy-MM-dd", new Locale("EN"));
SimpleDateFormat dateFormatter2 = new SimpleDateFormat("dd MMMM yyyy", new Locale("EN"));
//Convert to Date
Date dateToParse = dateFormatter1.parse("2024-01-01");
//Format OUTPUT date
String dateAsString = dateFormatter1.format(dateToParse);
System.out.println(dateAsString); //01 January 2024
//Convert OUTPUT date from STRING to DATE
Date dateToReturn = dateFormatter1.parse(dateAsString);
System.out.println(dateToReturn.toString()); //Mon Jan 01 00:00:00 SAST 2024
Note:
I used both DateFormatter and SimpleDateFormatter but got the same output.
The outputs are very different and what I am trying to achieve is to have my String created as a Date object in the exact same format.
I feel I am missing something but I just cannot figure out what.
The code I provided is a snippet from a bigger function that returns type Date
The function wasn't created by myself, I'm picking up from where someone else left off
Whenever you call a .toString() method on a Date, then the DateFormatter is not taken into account. The format of date you get is just a matter of default toString implementation of Date class.
To use formatter while printing, try something like this (instead of the last line in your snippet):
System.out.println(dateFormatter1.format(dateToReturn));
Date.toString() returns always returns a string in the format dow mon dd hh:mm:ss zzz yyyy.
You'll need to reuse the formatter's format method to get the initial string you expect
This question already has answers here:
String to Date Conversion mm/dd/yy to YYYY-MM-DD in java [duplicate]
(5 answers)
how to parse output of new Date().toString()
(4 answers)
Closed 3 years ago.
I have a date string "Wed Dec 02 00:00:00 ICT 2015". I tried to use SimpleDateFormat to convert this string to a date with format "YYYY-mm-DD". The code looks like this:
Date date = new SimpleDateFormat("YYYY-mm-DD").parse("Wed Dec 02 00:00:00 ICT 2015");
But I got an exception:
java.text.ParseException: Unparseable date: "Wed Dec 02 00:00:00 ICT 2015"
Most programming languages have a concept of "date/time" representation, which represents some point in time and allows the application of rules, such as time zones and leap years/seconds and other oddities to be applied.
When parsing a String value, you must know the format that the String is in, let's face it, what does 3/3/3 mean?
Java 8 replaced the existing Date/Calendar API which a much richer and less error prone API and you should make use of it as much as possible.
The first step is to construct a DateTimeFormatter of which represents the desired input format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
See the JavaDocs for more details on the specifiers.
Next, you want to parse the text using the formatter...
String text = "Wed Dec 02 00:00:00 ICT 2015";
ZonedDateTime zdt = ZonedDateTime.parse(text, formatter);
System.out.println(zdt);
nb: I've used a ZonedDateTime because I want to carry over the time zone information, you could use a LocalDateTime, but that would depend on your underlying needs
This will print 2015-12-02T00:00+07:00[Asia/Bangkok]
I expect that the date will have "YYYY-mm-DD" format
An important concept to get your head around is, the toString value of the a date object has nothing to do with its underlying representation and only represents a human readable representation of the object.
To format the date value into something else, you need to use another DateTimeFormatter...
String formattedDate = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(zdt);
System.out.println(formattedDate);
And the will print 2015-12-02
Does this serves your purpose:
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse("Wed Dec 02 00:00:00 ICT 2015");
//convert this date to the desired format
DateFormat target = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(target.format(date));
This question already has answers here:
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
Closed 5 years ago.
I'm working on an android application , when i need to send my current date to the server , i get my actual date by currentTime = Calendar.getInstance().getTime(); and i'm parsing it using SimpleDateFormat "yyyy/MM/DD" , but the value i got for day is very weird : 38
There is the code i'm using :
currentTime = Calendar.getInstance().getTime();
SimpleDateFormat df1 = new SimpleDateFormat("yyyy/MM/DD");
String formattedDate1 = df1.format(currentTime.getTime());
NB : The date value returned by currentTime is correct
PS : This code was working correctly last days !
The pattern matching for parsing dates is case sensitive. You need to be very careful when generating your parsing pattern and refer to the documentation.
DD, all capital/uppercase letters, refers to the current day in the year. Today, 7 February, is the 38th day in the year (31 in January + 7 in February.)
dd, all lowercase letters, refers to the current day in the month. This would parse as 7 for 7 February.
The correct pattern you should be using, therefore, is: yyyy/MM/dd.
It was working recently, because it was previously January. For the month of January only, dd and DD will return the same value.
I believe you want d not DD
D returns the current day in year (31 days in Jan + 7 in Feb, I'm assuming).
d returns current day in month.
sauce: https://developer.android.com/reference/java/text/SimpleDateFormat.html
This question already has answers here:
java date problem in parsing
(4 answers)
Closed 7 years ago.
I am getting a date by ajax in String format. But it is getting changed when I am converting it to date by SimpleDateFormat. The month is always changed to Jan. I am worried only about the month change.My code is given below
String appointmentDate = request.getParameter("appointmentDate");
System.out.println(" appointment date in String format "+appointmentDate);
Here I am getting the date correctly(16/12/2015). But when I am changing it to Date format it is getting changed(Fri Jan 16 00:12:00 IST 2015). Whatever I input the month, say August, May, June, I am always getting month Jan.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
Date parsedDate = dateFormat.parse(appointmentDate);
System.out.println(" appointment date in DATE format "+parsedDate);
Please help me out. Thanks in advance.
As per the JavaDoc, lower case m denotes minutes, not months.
Changing your expression to dd/MM/yyyy should fix the issue.
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 8 years ago.
I'm processing a list of dates from an input file, and I need to convert each from String to Date. Examples of the format:
9/2/2013 7:34:17 PM
1/13/2011 10:47:36 AM
Each time a line is read, the date is stored in the String variable dateAsString. Here's what I've got:
DateFormat format = new SimpleDateFormat("MM/dd/YYYY hh:mm:ss a");
Date myDate = format.parse(dateAsString);
System.out.println(myDate.toString());
The output is incorrect:
9/2/2013 7:34:17 PM becomes Sun Dec 30 19:34:17 EST 2012
1/13/2011 10:47:36 AM becomes Sun Dec 26 10:47:36 EST 2010
It seems pretty straightforward, so I'm confused. What am I doing wrong?
Just try 'yyyy' instead of 'YYYY'
Capital 'Y' means a "week year" where 'y' represents the actual year. Try using yyyy instead.
For more formatting help, check out the SimpleDateFormat API where they give you examples of the patterns to use.
Extra Info
Just in case you're wondering, a "week year" is a year where all the weeks in the year are whole weeks
Your format is not correct, as you can see in Javadoc :
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Use yyyy instead of YYYY for the year part