This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 7 years ago.
I am working with converting Date Strings to Epoch times then back again, but it seems like when I convert to Epoch the month is ignored when I parse the date String
String timeString = "15 Aug 2005 13:07:58.035";
Date dateTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("DD MMM yyyy hh:mm:ss", Locale.US);
try {
dateTime = sdf.parse(timeString);
} catch (ParseException e) {
e.printStackTrace();
}
long convert = dateTime.getTime();
System.out.println(str);
System.out.println(convert);
Date epochToString = new Date(convert);
System.out.println(epochToString.toString());
Here is the output
15 Aug 2005 13:07:58.035
1105812478000
Sat Jan 15 13:07:58 EST 2005
When I calculated the epoch time by hand the date was indeed Jan 15 2005, so for some reason the MMM value is being ignored in my format.
Edit:
I should also note that I changed the format token to MM and tried a numerical representation in the string instead, but had the same exact results.
This is because DD means day in year. You want dd for day in month. See SimpleDateFormat Javadoc
I suggest you use HH for 24 hour time.
Related
This question already has answers here:
Java Date Time conversion to given timezone
(3 answers)
Java - Unparseable date
(3 answers)
Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical
(4 answers)
Closed 3 years ago.
I have a problem. I've tried to parse a String which contains a date into another DateFormat.
Code:
String datumString = "Mon, 27 Jan 2020 21:31:16 +0100";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Date zeitstempel = null;
try {
zeitstempel = simpleDateFormat.parse(datumString);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(zeitstempel);
Error message:
java.text.ParseException: Unparseable date: "Mon, 27 Jan 2020 21:31:16 +0100"
at java.text.DateFormat.parse(DateFormat.java:366)
at de.puki.geopolitan.Main.<init>(Main.java:32)
at de.puki.geopolitan.Main.main(Main.java:73)
Please help. :)
The variable names show that you are German, most likely working on a system with German language and date formatting. By default this also affects SimpleDateFormat output and parsing (unless you specify the Locale to be used)!
Between the German and the English/US formatted date string there is a small but significant difference:
Locale.ENGLISH: "Mon, 27 Jan 2020 21:31:16 +0100";
Locale.GERMAN: "Mo., 27 Jan 2020 21:31:16 +0100";
The date string you parse however uses the English date format, therefore you have to explicitly set Locale.US or Locale.ENGLISHwhen constructing the SimpleDateFormat:
simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Afterwards you can parse the sample date string in your question.
This question already has answers here:
Java Date() giving the wrong date [duplicate]
(9 answers)
how to parse 'following string '20190911T14:37:08.7770400' into date format
(3 answers)
Parsing a string to date format in java defaults date to 1 and month to January
(2 answers)
Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2
(1 answer)
How to convert java.util.Date to java.sql.Date?
(17 answers)
Closed 3 years ago.
I was having some problem when trying to convert java.util.Date to java.sql.Date. My date parameter passed in was originally a string in this format: 2019-01-31. Then I am trying to convert it to java.util.date using this code:
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD");
Date date = null;
try {
date = formatter.parse(this._dDate);
} catch (ParseException e) {
e.printStackTrace();
}
In my SQL statement, I am trying to convert it to java.sql.Date:
buffer.append("SELECT * FROM TABLE1 WHERE LAST_LOGIN_DT < ");
buffer.append("TO_DATE('" + new java.sql.Date(date.getTime()) + "','YYYY-MM-DD') ");
However, when I printed out the buffer in string, I realized that the date is changed. For instance, the data string I passed in is 2018-01-01, but in the SQL statement it became 2017-12-31. Any ideas?
Thanks!
Let's take a look at following example first:
String date = "2018-01-01";
SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY-MM-DD");
System.out.println(sdf1.parse(date));
System.out.println(sdf1.parse(date).getYear());
System.out.println(sdf1.parse(date).getMonth());
System.out.println(sdf1.parse(date).getDay());
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.parse(date));
System.out.println(sdf2.parse(date).getYear());
System.out.println(sdf2.parse(date).getMonth());
System.out.println(sdf2.parse(date).getDay());
Console output:
Sun Dec 31 00:00:00 CST 2017
117
11
0
Mon Jan 01 00:00:00 CST 2018
118
0
1
Now I think you have already noticed the difference!
Quoted from Class SimpleDateFormat:
Y means Week year and D means Day in year.
y means Year and d means Day in month.
yyyy and YYYY both represent a year but yyyy represents the calendar year while YYYY represents the year of the week. And, dd represents the calendar day while DD represents the day of the month. Therefore, there are 2 ways to fix your issue:
Method 1
Change the format for SimpleDateFormat from YYYY-MM-DD to yyyy-MM-dd if you still want to convert this._dDate to java.util.Date then java.sql.Date, but I strongly recommend you don't do this.
Method 2
The better way is directly using java.sql.Date.valueOf(this._dDate) and the format of this._dDate is supposed to be yyyy-MM-dd.
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 4 years ago.
I have a user input a Date string in one format, i can parse it using the corresponding DateFormat and format it using another (DB) DateFormat to format the Date.
However, using the DB DateFormat, i cannot convert it to the corresponding Date object.
My code:
String userSpecifiedDate = "Fri Oct 12 15:28:04 UTC 2018";
SimpleDateFormat userSpecifiedDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'UTC' yyyy");
SimpleDateFormat DBDateFormat = new SimpleDateFormat("dd-MMM-yy hh.MM.ss.SSSSSS a");
Date userDateFormat = userSpecifiedDateFormat.parse(userSpecifiedDate);
System.out.println("User specified date in original format : " + userDateFormat);
// prints just fine : 'Fri Oct 12 15:28:04 PDT 2018'
String userSpecfiedDateInDBFormat = DBDateFormat.format(userDateFormat);
System.out.println("User specified Sting in DB format : " + userSpecfiedDateInDBFormat);
// prints: '12-Oct-18 03.10.04.000000 PM' (works for me, although see the minutes are wrong)
Date uss = DBDateFormat.parse(userSpecfiedDateInDBFormat);
System.out.println("User specified Date in DB format : " + uss);
// prints : 'Fri Oct 12 15:00:04 PDT 2018' ??
I was expecting it to print the date in the DB DateFormat, same as '12-Oct-18 03.10.04.000000 PM'
I need the Date in that format since i am going to use that Date Object in the JPA to compare dates in a #NamedQuery.
An RTFM question really - Java doc: docs.oracle.com/javase/8/docs/api/java/util/… The toString() method is fixed to use the "dow mon dd hh:mm:ss zzz yyyy" format
This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 4 years ago.
I am converting string to date format year and date showing correct but month showing different.
String s = "2018-08-29"
try
{
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD", Locale.ENGLISH);
Date parse = formatter.parse(s);
long time = parse.getTime();
}catch (ParseException e) {
e.printStackTrace();
}
output: Mon Jan 29 00:00:00 GMT+05:30 2018
The problem is in your format.
D stands for day in year, but you need to get day in month with d.
This should be like this:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Source
I hope it's helpful
Sorry I missed it at first. Your date should be simple letters "dd" not "DD"
DD is day in year
dd is date in month
This question already has answers here:
Converting 24hour time to 12hour time?
(6 answers)
Closed 6 years ago.
I just want to convert a date from the database to 12 hours format which is originally in 24 hours format which is something like this 2015-06-11T28:28:57.000Z. In here this time format (28:25:57) seems to be the next day (ie. 2015-06-12T04:28:57).
I have tried with the following:
String date = "2015-06-11T28:28:57.000Z";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss a");
Date date = formatter.parse(date);
but I got an error like unparsable date.
Your date formatter string was incorrect, Try this:
String dateStr = "2015-06-11T28:28:57.000Z";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date date = sdfInput.parse(dateStr);
System.out.println("Date:"+date ); //Fri Jun 12 04:28:57 IST 2015
// to get 12 hour date formate date-string from provided date
SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS ");
String dateStr2 = sdfOutput.format(date);
System.out.println("dateStr2:" + dateStr2); //2015-06-12 04:28:57.000