Wrong day value given by SimpleDateFormat [duplicate] - java

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

Related

Exact string to date and keep original format [duplicate]

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

java: ParseException: Unparseable date [duplicate]

This question already has answers here:
ParseException when parsing 3 character abbreviated month using SimpleDateFormat
(5 answers)
Closed 4 years ago.
i have a SimpleDateFormat format = new SimpleDateFormat("d M y H:m"); and i try to parse the String "8 Jan 2019 16:47" with it, but i get a ParseException. Did i create it the wrong way?
According to docs.oracle.com the M should recognize 3-letter-months.
Can anyone help me?
The official documentation: (https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
You probably missed out this little note here:
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
Based on your example input, the following works:
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm");
java.time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMM y H:mm", Locale.ENGLISH);
String stringToParse = "8 Jan 2019 16:47";
LocalDateTime dateTime = LocalDateTime.parse(stringToParse, formatter);
System.out.println(dateTime);
The output from this snippet is:
2019-01-08T16:47
What went wrong in your code?
SimpleDateFormat and Date are poorly designed and long outdated, the former in particular notoriously troublesome. I recommend you don’t use them in 2019.
As others have said you need three M for month abbreviation (no matter if you are using the outdated SimpleDateFormat or the modern DateTimeFormatter). One M will match a month number in 1 or 2 digits, for example 1 for January.
You should also specify a locale for your formatter. I took Jan to be English so specified Locale.ENGLISH. If you don’t specify locale, the JVM’s default locale will be used, which may work well on some JVMs and suddenly break some day when the default locale has been changed or you are trying to run your program on a different computer.
Link
Oracle tutorial: Date Time explaining how to use java.time.

Java parse date string returns wrong month [duplicate]

This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 4 years ago.
I try to parse a date string, but get wrong month, why?
new SimpleDateFormat("yyyy-MM-DD", Locale.US).parse("2018-03-08")
Why this returns month as Jan?
Please check screenshot:
Why not use the java.time API?
LocalDate localDate = LocalDate.parse("2018-03-08");
If you want to convert the LocalDate to a java.util.Date, you can follow this answer.
This is due to the format you used as "yyyy-MM-DD". The parser will parse in the sequence way:
Your input value is "2018-03-08"
yyyy - will bring to the year 2018
MM - will bring to the month MARCH
But what is DD? It's the number of the days from the beginning of the year.
So here it moved back to 8th day on this year (2018) which means January 8th.
That's why you are seeing January instead of March.
Looking at this tutorial if you are trying to display the month name you should use 3 M’s and isn’t the standard for the day to be d and not D
I would suggest trying yyyy-MMM-dd
I tried this below and it is working fine by changing the DD to dd
try {
Date date= new SimpleDateFormat("yyyy-MM-dd",Locale.US).parse("2018-03-08");
Calendar calendar= Calendar.getInstance();
calendar.setTime(date);
Log.d("MONTH ","" + calendar.get(Calendar.MONTH));
} catch (ParseException e) {
e.printStackTrace();
}
You need to use dd instead of DD
Try this :-
new SimpleDateFormat("yyyy-MM-dd", Locale.US).parse("2018-03-08");

Java not formatting date correctly [duplicate]

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");

Convert String to Date not working properly [duplicate]

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.

Categories