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.
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:
Julian day of the year in Java
(9 answers)
Closed 2 years ago.
Requirement : get the date in Julian format (ordinal date), compare it with current date, get the month and year out of it.
Convert it with output format.
Input format: yydddd
Output format: yymm
JDK 8
One way I can do is using date :
Date myDateWrong = new SimpleDateFormat("yyyyddd").parse("2020366");
Any cleaner way ?
java.time
looking for java.time.* solution which can parse yyyyddd format
That’s what I recommend too.
DateTimeFormatter dayOfYearFormatter
= DateTimeFormatter.ofPattern("uuuuDDD");
DateTimeFormatter yearMonthFormatter
= DateTimeFormatter.ofPattern("uuMM");
String yyyydddString = "2020366";
LocalDate date = LocalDate.parse(yyyydddString, dayOfYearFormatter);
String output = date.format(yearMonthFormatter);
System.out.println(output);
Output is:
2012
So year 2020 month 12.
What went wrong in your code?
Whether you use the modern DateTimeFormatter or the old and troublesome SimpleDateFormat, lowercase d is for day of month and uppercase D is for day of year. Why it worked with SimpleDateFormat anyway was because that class confusingly defaults month to January if no month is given. So your date was parsed into the 366th day of January. What?! That’s right, one more confusing trait of SimpleDateFormat, with default settings it happily parses non-existent dates. When there are only 31 days in January, it just extrapolates into the following months and ends up at December 31, the day you had intended. SimpleDateFormat is so full of nasty surprises like these. I recommend you never ever use that class again.
Link
Oracle tutorial: Date Time explaining how to use java.time.
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");
This question already has answers here:
How to determine day of week by passing specific date?
(28 answers)
Closed 5 years ago.
I'm working on a task where I need to provide specific day like Wednesday, Thursday etc on given input date like
Input:
6 7 2017
Output:
Wednesday
Any help would be appreciated thank you.
I have tried using java.util.Calendar. I am able to get output like "Sat Sep 05 08:18:27 UTC 2015" but I need only day of week.
I would follow these steps:
Parse the string to a LocalDate using a DateTimeFormatter
Use the LocalDate::getDayOfWeek method to find the day
Using java8 you will need a local date and the call the method getDayOfWeek(), which returns an enum with the info you need
example:
LocalDate date = LocalDate.now();
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println(dayOfWeek);
Try this
SimpleDateFormat format1=new SimpleDateFormat("MM/dd/yyyy");
Date dt1=format1.parse(pass your date);
DateFormat format2=new SimpleDateFormat("EEEE");
String finalDay=format2.format(dt1);
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");