I have very strange situation. My date is not changing after I am using parse() method from SimpleDateFormat. I tried to search on another questions on the site but without any success. This is my code:
String CALENDAR_NO_TIMEZONE = "dd-M-yyyy hh:mm:ss a";
String CALENDAR_FORMAT = "dd-M-yyyy hh:mm:ss a Z";
String dateValue = new SimpleDateFormat(CALENDAR_NO_TIMEZONE).format(scheduledDate);
Date scheduledDate = new SimpleDateFormat(CALENDAR_FORMAT, Locale.US).parse(dateValue + " " + timeZoneAbbr);
For example my value for dateValue is "17-2-2021 12:15:00 AM", for timeZoneAbbr is "ACT" and for scheduledDate is "Wed Feb 17 0:15:00 CET 2021" before parsing, but also after parsing everything is the same, like this parse is not working. So where I am wrong?
Thanks in advance.
Related
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 string to date conversion
(17 answers)
Closed 4 years ago.
So I have a Date variable which I formatted using the following code:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = new Date();
String date = format.format(currentDate);
I then put the String 'date' into my database.
Now: I have to pull it out of the database. It comes out as, of course, a String in the format shown above.
My question is how do I turn this String into a date variable? I'm very much struggling to find the most efficient way.
Thank you.
You can use a SimpleDateFormatter to parse a String in to a Date.
Simply use DateFormat#parse:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date inputDate = new Date();
System.out.println("inputDate: " + inputDate);
String dateString = format.format(inputDate);
System.out.println("dateString: " + dateString);
Date parsedDate = format.parse(dateString);
System.out.println("parsedDate: " + parsedDate);
Sample output:
inputDate: Fri May 04 23:56:54 GMT 2018
dateString: 2018-05-04 23:56:54
parsedDate: Fri May 04 23:56:54 GMT 2018
See working code on ideone.com.
I would like to convert my string date to int but I get an error, example of my date 'dim. janv. 23 24:00:00 +0000 2011`
This is part of my code :
String created_at = (String) jsonObject.get("created_at");
System.out.println("la date de création de tweet est: " + created_at);
DateFormat df = new SimpleDateFormat(" ddd. MMMM. EE HH:mm:ss z yyyy");
String s= df.format(created_at);
int out=Integer.valueOf(s);
System.out.println("new date " +out);
And the output is:
java.lang.IllegalArgumentException: Cannot format given Object as a Date.
Well, you already have the date in String format and that's what the format method does. I am assuming what you want to do here is to parse the date (into Date object) and not format.
Also, it looks like the date is in French locale, so you need to use appropriate locale along with SimpleDateFormat and use parse method, e.g.:
DateFormat df = new SimpleDateFormat("EEE MMMM dd HH:mm:ss z yyyy", Locale.FRANCE);
Date date = df.parse("dim. janv. 23 24:00:00 +0000 2011");
System.out.println(date);
This would give you the Date object. If you want to format it differently, you can call format method with different format.
Update
Also, it looks like you are calling overloaded version of format method (by passing in a String and not a Date object. This evantually calls format method of TextFormat class (javadoc here) and that's why you get that Exception.
I am aware that there is an accepted answer already. I am posting this to inspire you and anyone else to drop the outdated classes DateFormat and SimpleDateFormat. These days we have so much better in the modern DateTimeFormatter and friends, these classes tend to be much more programmer friendly. So use these if you can — which you can.
System.out.println("la date de création de tweet est: " + created_at);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
Locale.FRENCH);
OffsetDateTime dateTime = OffsetDateTime.parse(created_at, dtf);
This prints:
la date de création de tweet est: dim. janv. 23 24:00:00 +0000 2011
2011-01-24T00:00Z
In French the dots are part of the abbreviations for day of week and month, so they should not be explicit in the format pattern string. Also be aware that you don’t have a leading space in there (unless your date-time string has one too). The result is correct since midnight at 24 hours on 23th of January is the same as 0 hours in the 24th.
The other string from the comment, "Tue Feb 08 12:30:27 +0000 2011", is in the same format, only in English. So you need not change the format pattern, only the locale:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
Locale.ENGLISH);
Now the result is:
la date de création de tweet est: Tue Feb 08 12:30:27 +0000 2011
2011-02-08T12:30:27Z
I didn’t understand the part about converting to integer. I noticed you tried Integer.valueOf(s) in the code in the question, which will only work if you format the date-time into a string of digits first. For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMdd");
String s = dateTime.format(formatter);
int out = Integer.parseInt(s);
System.out.println("new date " + out);
This prints
new date 20110124
But I probably guessed incorrectly at what you are aiming at. I shall be happy to edit if you explain your requirement better. I used parseInt() en lieu of valueOf(), the result is the same, I just avoid the automatic conversion from Integer to int.
Edit: What if the date of creation of tweet is for example "dim. janv. 23 24:00:10 +0000 2011"? That is, 10 seconds past midnight. Then we get Invalid value for HourOfDay (valid values 0 - 23): 24.
While Java apparently accepts 24:00:00 as a time, it thinks that times after midnight should written as for example 0:00:10 on the following day. However, we can easily relax that requirement:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss xx uuuu",
Locale.FRENCH)
.withResolverStyle(ResolverStyle.LENIENT);
Date time formatters come with three resolver styles, strict, smart (the default) and lenient. Using the last one we get
2011-01-24T00:00:10Z
Again, as expected, 24:00:10 on 23th of January equals 0:00:10 on the 24th.
How can we remove and preserve only Date from a String:
For ex: String outPut = {Time = 0:0:0} {Timestamp = Tue Oct 20 23:54:10 BST 2013}
I would like to have only Date
For ex: Tue Oct 20 2013
So far, I have tried the following approach:
String[] manipulateDate = output.getDate().split("\\{");
for(String s : manipulateDate ){
String outputDate = manipulateDate [2].replaceAll("\\}", "").replaceAll("\\s*\\bTimestamp =\\b\\s*","");
System.out.println(outputDate );
}
What is the best way to implement and use Java Date/String?
To strictly answer your question, you could do:
String date = output.replaceAll(".*Timestamp = (.*)? \\d{2}:.*?(\\d{4}).*", "$1 $2");
But it may be preferable to parse the whole string as a date object:
String output = "{Time = 0:0:0} {Timestamp = Sun Oct 20 23:54:10 BST 2013}";
String timestamp = output.replaceAll("\\{.*?\\} \\{Timestamp = (.*)\\}", "$1"); //Sun Oct 20 23:54:10 BST 2013
ZonedDateTime dateTime = ZonedDateTime.parse(timestamp, DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH));
From that point on, you can do what you want with the date object, including printing it as a string. Note that I changed the original string to Sunday (vs. Tuesday in your question): parsing it as a date also caught the fact that the 20th of October 2013 was not a Thursday, another advantage over strings.
I have to compare 2 dates which are in String format as: Fri Aug 23 17:03:19 IST 2013
I am trying to use new SimpleDateFormat("dd-MM-yyyy HH-mm-ss") to convert in DateTime so that dates can be comparable .
tempTimeStamp=Fri Aug 23 17:03:19 IST 2013
DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
Date startDate;
startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);
But its showing error object of this type can't be converted in DateFormat.Please help..
You were using the wrong format to try and parse the datetime string, try using the following snippet:
String tempTimeStamp="Fri Aug 23 17:03:19 IST 2013"
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date startDate = startDate = df.parse(tempTimestamp);
String newDateString = df.format(startDate);
You can look up more on the patterns for defining the date time format at http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Your expected format doesn't matches with the one provided.
The format of your SimpleDateFormat does not match that of the String that you are using.
The input String should match dd-MM-yyyy HH-mm-ss
If you are wanting simply to format the output of a Date, then no parsing of a String is necessary.