This question already has answers here:
Java SimpleDateFormat returns unexpected result
(3 answers)
Closed 6 years ago.
I am using java.text.SimpleDateFormat in Scala to convert string to date.
val isdf = new SimpleDateFormat("dd/MM/yyyy")
isdf.parse("01/22/2016") gives Sun Oct 01 00:00:00 UTC 2017
How to fix this ? Are there any alternative?
Your problem is that you have "22" in the month position. The date format you set is day/month/year, but it looks like the date you sent is in the format month/day/year.
The formatting is different between the dates. You have mixed the month and Day up in your statements. try this:
val isdf = new SimpleDateFormat("MM/dd/yyyy");
goodluck with the code.
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:
Convert String to java.util.Date
(4 answers)
Closed 7 years ago.
I read more questions on the web but I dont' find a solution yet.
I have a String like "14/05/1994", exactly in this format.
I need to covert it into java.util.Date in the same format.
I tried:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
But the result is: Sat May 14 00:00:00 CET 1994
It's possibile have as a result: 14/05/1994 not as a String, but as java.util.Date?
A java.util.Date doesn't have a format. It's just a date.
When you print it out, e.g. using toString(), it uses a default format, which is what you're seeing. But you have that date.
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
I don't think that can be your code because DateFormat.format accepts a Date and returns a String, not the other way around. You might mean df.parse, which would get you the results you describe. But if you take your SimpleDateFormat and use its format method on the Date, then you should get back out 14/05/1994 as you want. A java.util.Date doesn't have a format, though.
This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Closed 7 years ago.
I want to create a parser program. Every two characters in string will cut into a unit of time, for example: "150901184610801" need to be converted to displayed as "Tue Sep 01 18:46 CST 2015".
I know my question is somewhat complex, so I would like to be divided into different blocks sequentially solve the problem.
The first problem is:
How to use JAVA to format string follow a rule : each two word element cut into a variable, so I can manipulate variables such as yy (15), mm (09), dd (01), hh (18), ss (46).
Thanks again for any suggest.
Actually seems quite simple:
String s = "150901184610801";
SimpleDateFormat df = new SimpleDateFormat("yyMMddhhmmssS");
System.out.println(df.parse(s));
OUTPUT:
Tue Sep 01 18:46:10 CEST 2015
Check SimpleDateFormat API
You can use a SimpleDateFormat
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:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
I have the following string: Feb 16, 2014 2:11:41 PM
I have Googled around for some time now and only see examples on converting strings in the format mmddyyyy. I can't seem to find out how to convert the format above.
How can I convert this to a Date.
Thanks,
Gary
Use below formatter:
SimpleDateFormat formatter = new SimpleDateFormat("EEE dd,yyyy HH:mm:ss aa");
Also this link would be very helpful