I have dates in following formats
EEEE MMM dd, yyyy HH:mm aaa
EEE, dd MMM yyyy HH:mm:ss z
EEE, MMMM dd, yyyy, HH:mm aaa z
yyyy-MM-dd-HH-mm
yyyyMMddHHmm
EEEE, MMMM dd, yyyy HH:mm aa
yyyyMMddHHmmssSS
yyyyMMddHHmmss
MM/dd/yyyy HH:mm
I want these dates to be in my local GMT, and convert from one GMT to other GMT dynamically in java.
Is there any native java function to this or i have to code it.
e.g convert 11/07/2013 12:32pm (MM/dd/yyyy HH:mm) to GMT+5 timezone
i mean to say that if my datetime is in GMT-10 timezone, that what will be its value in GMT+5 timezone
Any help will be appreciated. Thanks
private String converttogmt(Date date){
SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
datef.setTimeZone(TimeZone.getTimeZone("GMT"));
return datef.format(date);
}
Try this one.
Related
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));
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", };
I am getting the above error, but to me everything seems to be correct.
What I am doing wrong?
DateTimeFormatter simpleDateFormatInput= DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
DateTime datetime = simpleDateFormatInput.parseDateTime(pubDate);
Where pubDate is Sat, 30 Jan 2016 12:23:53 +0100
The day and/or month from your input String may not match those from your default Locale. Try
DateTimeFormatter simpleDateFormatInput =
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z").withLocale(Locale.US);
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());
How can I parse a pubDate from a RSS feed to a Date object in java.
The format in the RSS feed:
Sat, 24 Apr 2010 14:01:00 GMT
What I have at the moment:
DateFormat dateFormat = DateFormat.getInstance();
Date pubDate = dateFormat.parse(item.getPubDate().getText());
But this code throws an ParseException with the message Unparseable date
You can define the date format you are trying to parse, using the class SimpleDateFormat:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date date = formatter.parse("Sat, 24 Apr 2010 14:01:00 GMT");
Additionally, for non-English Locale's, be sure to use the following when parsing dates in English:
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
If you need to have an RFC822 compliant date, try this :
DateFormat dateFormatterRssPubDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
For the lucky one that can use the Java 8 LocalDateTime:
LocalDateTime localDateTime = LocalDateTime.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse("Sat, 24 Apr 2010 14:01:00 GMT"));