"EEEE, MMMM d, yyyy 'at' HH:mm" - java

I am trying to convert from string to date. An example is :
Wednesday, September 4, 2013 at 5:07pm
I first convert it to:
Wednesday, September 4, 2013 at 5:07 PM
And use the following code:
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mm a");
But I always get the Unparseable date Exception.
Any help is appreciated!

The following works for me:
String str = "Wednesday, September 4, 2013 at 5:07 PM";
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mm a", Locale.US);
But if I remove the Locale, then I get a ParseException. Your computer Locale corresponds probably not to a english speaking locale.

This seems to work fine:
SimpleDateFormat in = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mma");
Date date = in.parse("Wednesday, September 4, 2013 at 5:07pm");
SimpleDateFormat out = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mm a");
String result = out.format(date);
System.out.println(result);

Why not use 'dd' instead of 'd'
So, instead of:
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mm a");
Use:
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' hh:mm a");

Related

DateTime format needed chnages as expected [duplicate]

This question already has answers here:
Java date format - including additional characters
(5 answers)
Closed 2 years ago.
I want to display time as "02 Sep 2020 at 12:24 AM" (mind the at between date and time).
The current format I am using is "dd MMM yyyy hh:mm aaa",
which displays time as "28 Aug 2020 11:32 AM".
How can I put an at before the time?
You can add string literals to a date format by surrounding them with single quotes ('):
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy 'at' hh:mm aaa");
// Here -------------------------------------------------^--^
String formatted = sdf.format(myDateVariable);
If you use java.time for this, you can define a java.time.format.DateTimeFormatter to parse a String, use it to parse the String to a java.time.LocalDateTime and define & use another DateTimeFormatter that includes the at escaping it in the pattern by enclosing it in single-quotes:
public static void main(String[] args) {
String dateTime = "02 Sep 2020 12:24 AM";
DateTimeFormatter parserDtf = DateTimeFormatter.ofPattern("dd MMM uuuu hh:mm a",
Locale.ENGLISH);
DateTimeFormatter outputDtf = DateTimeFormatter.ofPattern("dd MMM uuuu 'at' hh:mm a",
Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(dateTime, parserDtf);
System.out.println(ldt.format(outputDtf));
}
This code produces the output
02 Sep 2020 at 12:24 AM
Just wrap the word in single quotes.
"dd MMM yyyy 'at' hh:mm aaa"
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));

In JAVA, how do you calculate time difference between two dates in string format where both the times are in different time formats and time zones?

I have two strings containing Dates in different formats.
For example :
String Date1 = "Fri, 25 Jan 2019 11:34:11 GMT";
String Date2 = "Thu, 24 Jan 2019 17:14:21 EST";
In JAVA, how do I calculate the time difference between them given that both of them have different timezone (like GMT and EST) above?
Also, note that Date1 and Date2 can be in any of the below-given formats :
private static final String[] formats = {
"EEE, d MMM yyyy HH:mm:ss Z", "EEEE, MMMM d, yyyy, HH:mm Z",
"EEE, d MMM yyyy HH:mm:ss z",
"yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm:ss",
"MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'",
"MM/dd/yyyy'T'HH:mm:ss.SSSZ", "MM/dd/yyyy'T'HH:mm:ss.SSS",
"MM/dd/yyyy'T'HH:mm:ssZ", "MM/dd/yyyy'T'HH:mm:ss",
"yyyy:MM:dd HH:mm:ss", "yyyyMMdd", };

"Unparsable date" when trying to reformat a date String

I want to reformat a given date String into a different format:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' KK:mm aa zzzz");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = simpleDateFormat.parse(question.getOccur());
String formattedTime = output.format(d);
I'm getting this exception:
java.text.ParseException: Unparseable date: "Monday, December 7, 2015 at 12:05:13 PM Eastern Standard Time" (at offset 33)
You missed the seconds from your date format.
Try this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' KK:mm:ss aa zzzz");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = simpleDateFormat.parse(question.getOccur());
String formattedTime = output.format(d);
Edit. For the following example: Thursday 12/10/2015 01:35 AM, the date format is this:
SimpleDateFormat output = new SimpleDateFormat("EEEE dd/MM/yyyy hh:mm a");

unexpected output while converting a string to date in java

I have a string "12/9/2010 4:39:38 PM" which i have to convert to a date object. I am using the following code to do it:
String str = "12/9/2010 4:39:38 PM";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
date =(Date)formatter.parse(str);
System.out.println("date printed"+date);
However, when im printing the output, i see
Thu Dec 09 04:39:38 IST 2010
How do I get the date exactly the way I declared in the string i.e
12/9/2010 4:39:38 PM
as output? Pls help
You're assuming that the Date value itself remembers the format - it doesn't. Date.toString will do what it wants - because the Date only represents an instant in time.
If you want to format a Date, use your formatter again:
System.out.println(formatter.format(date));
However, that won't necessarily return the exact same value that was in your string, as there may be multiple values which parse the same way. For example, as you've only used "H:m:s", I'd expect "4:5:6" to be parsed the same way as "04:05:06".
You can entirely specify the format of your date output using the class Formatter
Short answer
String str = "12/9/2010 4:39:38 PM";
Formatter formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
Date date =(Date)formatter.parse(str);
Formatter formatterOutput = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
String s = formatterOutput.format(date);
Other examples
Format formatter;
// The year
formatter = new SimpleDateFormat("yy"); // 02
formatter = new SimpleDateFormat("yyyy"); // 2002
// The month
formatter = new SimpleDateFormat("M"); // 1
formatter = new SimpleDateFormat("MM"); // 01
formatter = new SimpleDateFormat("MMM"); // Jan
formatter = new SimpleDateFormat("MMMM"); // January
// The day
formatter = new SimpleDateFormat("d"); // 9
formatter = new SimpleDateFormat("dd"); // 09
// The day in week
formatter = new SimpleDateFormat("E"); // Wed
formatter = new SimpleDateFormat("EEEE"); // Wednesday
// Get today's date
Date date = new Date();
// Some examples
formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
// 01/09/02
formatter = new SimpleDateFormat("dd-MMM-yy");
s = formatter.format(date);
// 29-Jan-02
// Examples with date and time; see also
// Formatting the Time Using a Custom Format
formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
s = formatter.format(date);
// 2002.01.29.08.36.33
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
s = formatter.format(date);
// Tue, 09 Jan 2002 22:14:02 -0500
from: http://www.exampledepot.com/egs/java.text/formatdate.html
Use the same formatter:
System.out.println("date printed "+ formatter.format(date));
Converting it back to String using SimpleDateFormat?
formatter = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
String temp =formatter.format(date );
Java.util.Date has no concept of an intrinsic format - You need to use the format(java.util.Date d) method to see a formatted String representation of your Date object.
String str = "12/9/2010 4:39:38 PM";
DateFormat formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
Date date =(Date)formatter.parse(str);
System.out.println("date printed"+formatter.format(date));
Not sure what you are trying to accomplish. But you'll have to call SimpleDateFormat.format() to get what you are expecting. Printing the date directly will get only toString() implementation of Date
public static void main(String args[])
{
String string="2012-09-13";
Date str=processFileDate(string);
System.out.println(str);
}
public static Date processFileDate(String str)
{ //returns the date or "null" if doesn't exist
String[] strformat={
"EEE,dd MMM yyyy","MMM dd, yyyy, hh.mmaa zzz",
"EEEE, MMMMM dd yyyy 'at' hh:mm",
"EEEE, MMMMM dd, yyyy, hh:mm",
"EEE MMM dd yyyy, hh:mm ",
"dd MMMMM yyyy'Last updated at' hh:mm zzz",
"MMM dd, yyyy 'at' hh:mmaa",
"MMM dd, yyyy 'at' hh:mmaa zzz",
"MMMMM dd, yyyy, hh:mm aa zzz",
"EEE, MMM dd, yyyy hh:mm ",
"MMMMM dd, yyyy hh:mm zzz",
"MMMM dd, yyyy hh:mm aa",
"MMMM dd, yyyy hh:mmaa",
"MMMM dd, yyyy hh:mm",
"dd MMMM yyyy hh:mm:ss",
"dd MMMM yyyy hh:mm",
"MMMM dd, yyyy",
"dd MMMM yyyy ",
"dd MM yy",
"yyyy MMMM dd",
"dd'st' MMMM,yyyy",
"dd'nd' MMMM,yyyy",
"dd'rd' MMMM,yyyy",
"MMMM dd,yyyy",
"MMM dd yy",
"mm dd yy",
"yyyy-MM-dd",
"yyyy-MM-dd HH:mm:ss",
"E MMM dd hh:mm:ss Z yyyy",
"EEE, dd MMM yyyy HH:mm:ss Z"
};
String temp="null";
for(int i=0;i<str.length();i++){
temp=str.substring(i, str.length());
for(int l=0;l<strformat.length;){
Date strp=checkformat(temp,strformat[l]);
if(strp!=null)
{
return strp;
}
else l++;
}
}
return null;
}
private static Date checkformat(String str, String sdf) {
SimpleDateFormat sdformat=new SimpleDateFormat(sdf);
try{
Date d=sdformat.parse(str);
return d;
}catch(Exception e){}
return null;
}

Parse date in android

I have the following code to pase a date
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
Date _pubDate = df.parse(_pubDateE.getFirstChild().getNodeValue());
But I get this error:
java.text.ParseException: Unparseable date: "Fri, 12 Aug 2011 15:34:47 CEST"
What is wrong ?
You're missing the timezone in the date format at the end, in your exception message, the "CEST" part.
Your code
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
should be
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss z", Locale.getDefault());
You might want to read SimpleDateFormat
Edit
At the bottom of this page, the timezone format is more cleary explained
Clearer Timezone format
I think you need to add zzz in the end (for timezone):
"EEE, dd MMM yyyy kk:mm:ss zzz"
The date string is not in the format you specified. Notice the time zone at the end?
You probably want: new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz", Locale.getDefault());

Categories