This is the string that I have:
Sat, Nov 02, 2013 at 5:10 pm
I'm trying to parse it into a date time using this formatter:
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, YYYY 'at' K:mm a");
However, this is what it returns when I use it to parse the date string:
Sat Jan 05 17:10:00 CST 2013
I assume I'm getting the formatter wrong, but I can't figure out where.
Capital YYYY is the format for something called the "week year". You want the lowercase yyyy for the actual year.
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, yyyy 'at' K:mm a");
With this change I output the parsed date and get:
Sat Nov 02 17:10:00 PDT 2013
(I'm in the Pacific time zone.)
Related
need help in java code to get current date and time in below format :
newdate = "Mon, 13 Jul 2020 14:08:30 GMT"
After this I need to replace current date with earlier one:
vJsonfile1.replace(earlierdate," "+ newdate);
You can use ZonedDateTime and the RFC_1123 format to get the output you need:
DateTimeFormatter dtf = DateTimeFormatter.RFC_1123_DATE_TIME;
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("GMT"));
System.out.println(dtf.format(zdt));
Wed, 15 Jul 2020 19:07:37 GMT
Note the 1123_DATE_TIME format doesnt play nice with North American Time zones so itll work as long as its GMT or European time zones otherwise below will suffice too:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z");
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(dtf.format(zdt));
Wed, 15 Jul 2020 14:13:22 CDT
Which will output the current time with the time zone its in.
Here is an example that formats your datetime and then changes the date portion of it:
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime.format(DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z")));
zonedDateTime = ZonedDateTime.of(LocalDate.of(2020, 12, 15), zonedDateTime.toLocalTime(), zonedDateTime.getZone());
System.out.println(zonedDateTime.format(DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z")));
This code does exactly what you wanted it to do.
Output:
Mi, 15 Jul 2020 08:55:21 MESZ
Code:
SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z");
Date currentDate = new Date();
System.out.println(formatter.format(currentDate));
Here in my code I get the current time and than format it according to "UTC" timezone and than parsed it using the same Timezone hence converting it to a Date. The problem is that I get the date as "Wed Jul 20 13:04:51 GMT+05:00 2016" than
its formatted and parse as in the code below
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
//gives time as "Wed Jul 20 13:04:51 GMT+05:00 2016"
dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateText = dateFormat.format(currentDate);
//This formats dates as following "Wed, 20 Jul 2016 08:04:51 +0000"
Now uptil here things work fine, the time with timezone GMT+05:00 is converted to standard UTC with GMT+00:00 that shown by +0000 in the formatted date
Date setteledDate = dateFormat.parse(dateText);
Now Parsing the date as above gives the following date.
"Wed Jul 20 13:04:51 GMT+05:00 2016". The problem is this date is again in GMT+5
and the whole purpose of me getting current date formatting it and than parsing it is lost because the purpose was to get current date according to "UTC" (GMT+00:00).
Please help.
Replace Z to z in SimpleDateFormat format.
Calendar calendar = Calendar.getInstance();
Date currentDate = calendar.getTime();
//gives time as "Wed Jul 20 13:04:51 GMT+05:00 2016"
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateText = dateFormat.format(currentDate);
Log.e("date",dateText);
//output Wed, 20 Jul 2016 09:01:20 UTC
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
dateText = dateFormat.format(currentDate);
Log.e("date",dateText);
//output Wed, 20 Jul 2016 09:02:04 GMT+00:00
//update
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
try {
Date date = dateFormat.parse("Wed Jul 20 13:04:51 GMT+05:00 2016");
Log.e("date",date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
How do I parse this date format:
"Tue Dec 16 07:01:31 CET 2014"
I tried the following:
DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL, Locale.CANADA);
But that doesn't seem to be correct. Do you recognize this format and know how I need to configure DateFormat?
Try this;
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
Date d = parserSDF.parse("Tue Dec 16 07:01:31 CET 2014");
I want to convert : Thu Feb 02 00:00:00 WET 2012 to 02/02/2012 (with date type not string) using JAVA.
I did
String date = "Thu Feb 02 00:00:00 WET 2012";
SimpleDateFormat formatnow = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);
SimpleDateFormat formatneeded=new SimpleDateFormat("YYYY-MM-dd");
Date date1 = formatnow.parse(date);
String date2 = formatneeded.format(date1);
Date date3= formatneeded.parse(date2);
System.out.println(date3);
And I'm having : Thu Feb 02 00:00:00 WET 2012.
Can anyone tell me where is the problem ??
The Date object does not hold any information about the display format you want. So parsing a date from a formatted date string is not going to 'remember' any formatting.
System.out.println(date3) will print value of date3 using java's toString method.
You have the formatted date string in date2. So System.out.println(date2) should give you the right value.
This is the date format that I need to deal with
Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)
But I don't get what the last two parts are. Is the GMT-0700 fixed? Should it be something like this?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT-0700' (z)");
No, it is not fixed. It is a TimeZone. You can match it with Z in the date format.
To be more precise, in SimpleDateFormat formats :
Z matches the -0700 part.
GMT is fixed. Escape it with some quotes.
z matches the PDT part. (PDT = Pacific Daylight Time).
The parenthesis around PDT are fixed. Escape them with parenthesis.
You can parse your date with the following format :
EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'
Another remark : Wed Aug contains the day and month in English so you must use an english locale with your SimpleDateFormat or the translation will fail.
new SimpleDateFormat("*format*", Locale.ENGLISH);
Here is the Javadoc:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
For this example: Wed Aug 21 2013 00:00:00 GMT-0700 (PDT), you'd want this format:
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaDate {
public static void main (String[] args) throws Exception {
String s= "Wed Aug 21 2013 00:00:00 GMT-0700 (PDT)";
SimpleDateFormat sdf =
new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'z '('Z')'");
Date d = sdf.parse (s);
System.out.println ("Date=" + d + "...");
}
}
EXAMPLE OUTPUT: Date=Tue Aug 20 23:00:00 PDT 2013...
Thanx to Arnaud Denoyelle above for his edits!