I am getting an exception when parsing date 20160327020727 with format yyyyMMddhhmmss. Note that the lenient is set to false.
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
df.setLenient(false);
try {
Date dt = df.parse("20160327020727");
} catch (ParseException e) {
e.printStackTrace();
}
It is parsing other dates with the same format and it is working as expected. Why is this happening?
CET changes to summer time the last Sunday of march, so there is no 2AM this day.
You go from 1:59 to 3:00
You are getting an error because that time does not exist in your default time zone.
Try setting the timezone to UTC by doing df.setTimeZone(TimeZone.getTimeZone("UTC"));
In CET on the last Sunday of march it changes to summertime -> No 2AM on that day.
Change it to "yyyyMMdd HHmmss", so you can parse it easily.
Related
I have to convert UTC timestamp data to EST timezone. Below code is working fine when timezone difference is -5Hrs but when I give UTC time like - 2018-04-15T21:27:31.000Z then it outputs as 2018-04-15 16:27:31 -0500 which is not correct. Output should be 2018-04-15 17:27:31 -0400. It always subtract -5hrs.
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000'Z'");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat estFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
estFormat.setTimeZone(TimeZone.getTimeZone("EST"));
try {
String date1 = estFormat.format(utcFormat.parse("2018-04-15T21:27:31.000Z"));
System.out.println("est time : "+date1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You seem to expect the time zone shorthand named "EST" to obey the daylight saving time change rules. It doesn't. "EST" is the name for a time zone which is GMT-5 at any time of the year.
To get time zone definitions that obey daylight saving time rules as expected, you'll be better off using the name of the main cities that use these time zones.
In your case, try "America/New_York"
I have the following code
protected void amethod1() {
String strDate = "Thu May 18 16:24:59 UTC 2017";
String dateFormatStr = "EEE MMM dd HH:mm:ss zzz yyyy";
DateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
Date formattedDate = null;
try {
formattedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
The resulting value of formattedDate is- "Thu May 18 11:24:59 CDT 2017" .
I am testing this code in Chicago and the local timezone is CDT.
I am not able to understand why the time value changes from 16:24:59 to 11:24:59 even though. Am I missing something in the defined format of the date?
Class Date doesn't contain any timezone at all. It's just a number of milliseconds since 01.01.1970 00:00:00 GMT. If you try to see, what formattedDate contains with System.out.println or debugger, you'll get formatted date for your local timezone. 11:24:59 CDT and 16:24:59 UTC are the same time, so result is correct.
Is java.util.Date using TimeZone?
It is better to use jodatime or Java 8 Time API in order to better manage time and timezones.
First, you are getting the correct time. When Daylight Savings Time is in use in Chicago (which it is on May 18), the time is 11:24:59 when it’s 16:24:59 in UTC. So your Date value represents the same point in time. This is all you can expect from a Date.
I understand that you want not just a point in time, but also the UTC time zone. Since Axel P has already recommended Java 8 date and time API, I just wanted to fill in the details:
DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern(dateFormatStr, Locale.US);
ZonedDateTime dateTime = ZonedDateTime.parse(strDate, parseFormatter);
The result is
2017-05-18T16:24:59Z[UTC]
If you always want the UTC time zone, the Instant class is just right for it, so you will probably want to convert to it:
Instant instant = dateTime.toInstant();
Instants are always in UTC, popularly speaking.
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now=new Date();
System.out.println(myFmt.format(now));
I hope I can help you. If you can,please adopt.Thank you
The resulting value of formattedDate is- "Thu May 18 11:24:59 CDT 2017" . Why? because your time zone running -5 hour from UTC time you will find in below link wiki time zone abbreviations, if you want result in same timezone you need to specify timezone in formater Hope you get my concern
https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations
public static void amethod1() {
String strDate = "Thu May 18 16:24:59 UTC 2017";
String dateFormatStr = "EEE MMM dd HH:mm:ss zzz yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date formattedDate = null;
try {
formattedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("formattedDate: "+dateFormat.format(formattedDate));
}
You specified timezone, that's why after parsing time on current timezone (where you are), SimpleDateFormat sets UTC timezone. When you try to output your date, it is displayed on your current timezone
It appears you would need to specify the TimeZone as well when you format the Date For eg. .TimeZone.setDefault(TimeZone.getTimeZone("PST"));
Have a look at this discussion TimeZone
The output of a Date depends on the format specified, where you can specify the timezone, as shown in the example below:
protected void amethod2() {
String strDate = "Thu May 18 16:24:59 UTC 2017";
String dateFormatStr = "EEE MMM dd HH:mm:ss zzz yyyy";
DateFormat dateFormat = new SimpleDateFormat(dateFormatStr);
Date formattedDate = null;
try {
formattedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Date: " + formattedDate);
// Thu May 18 17:24:59 BST 2017, BST is my system default timezone
// Set the time zone to UTC for the calendar of dateFormat
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("Date in timezone UTC: " + dateFormat.format(formattedDate));
// Thu May 18 16:24:59 UTC 2017
// Set the time zone to America/Chicago
dateFormat.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
System.out.println("Date in timezone America/Chicago: " + dateFormat.format(formattedDate));
// Thu May 18 11:24:59 CDT 2017
}
As for the IDs, such as "UTC" and "America/Chicago" in the example, you can get a complete list of them via TimeZone.getAvailableIDs(). You can print them out to have a look:
Arrays.stream(java.util.TimeZone.getAvailableIDs()).forEach(System.out::println);
And you'll have:
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
...
I want Current Date in GMT wise Timezone.I used following code.
public static Date getGMTDate(String dateFormat) throws ParseException
{
SimpleDateFormat dateFormatGmt = new SimpleDateFormat(dateFormat);
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
// Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat(dateFormat);
// Time in GMT
return dateFormatLocal.parse(dateFormatGmt.format(new Date()));
}
//call above function.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = getGMTDate("yyyy-MM-dd HH:mm:ss");
when i will change my device date then time is display in GMT formate but Date is not display in GMT timezone.its display of device's date.
but I want Current GMT Date.
This may works
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(dateFormatGmt.format(new Date())+"");
Specify the format, and you will get it in GMT!
EDIT: You can also check This
I changed your method as below
public static String getGMTDate() {
DateFormat dateFmt = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.MEDIUM, SimpleDateFormat.MEDIUM);
dateFmt.setTimeZone(TimeZone.getTimeZone("GMT"));
// Time in GMT
return dateFmt.format(new Date());
}
And got the following date (which is same as what you wanted I believe)
07-18 10:41:54.525: D/test(6996): GMT Date is: **Jul 18, 2013 10:41:03 AM**
So simply call this method and it will return the GMT date in string format.
EDIT 1:
you are setting GMT time zone and not GMT Date. You are not trying to understand your code. When you called new Data() it returned your device date, and then you formatted it for the corresponding GMT date and time. So if your change your device date to 16th July then your code (or even my code) will return the GMT equivalent of 16th July time. If you want 18th July GMT time even when your device's date is 16th July 2000, then I don't think you can do it without getting it from some network entity.
EDIT 2:
You should look at this SO reference to a similar kind of problem and it may serve you well.
Output: 2016-08-01 14:37:48 UTC
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Works fine.
I get a date string from the server in EST so I convert it
example date 2013-04-16T11:56:07.15
incidentDate = l.item(0).getTextContent();
DateFormat dformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.US);
dformat.setTimeZone(TimeZone.getTimeZone("America/New York"));
Date timestamp;
try
{
timestamp = dformat.parse(incidentDate);
incidentDateLong = timestamp.getTime();
}
catch (ParseException e1) {
e1.printStackTrace();
}
the timestamp that gets returned is 1366113367015
If I plug that into a converter on this website to check the date
http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/
the milliseconds does not seem to be the correct date, it gives me Tue Apr 16 2013 07:56:07 GMT-0400 (Eastern Daylight Time) which is not was was sent to me from the server.
When I go to convert the date back it pulls the date back even further away from the actual date
Date incDate = new Date(dateInMili);
DateFormat dformat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a",Locale.US);
String dateStr = dformat.format(incDate);
Is something wrong with my formatter? I dont understand the problem
This is the problem:
TimeZone.getTimeZone("America/New York")
That's not a valid time zone ID. You want:
TimeZone.getTimeZone("America/New_York")
Note the underscore. Personally I think it's a shame that getTimeZone gives no indication that it hasn't actually found the time zone you've asked for, but it's been that way for a long time :(
I'm trying to parse a date from a String and get the long value. The long value will be later sent to an SQL query.
here's my code:
String dayDate = "28-02-2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date day = new Date();
try {
day = sdf.parse(dayDate);
} catch (ParseException pe) {
pe.printStackTrace();
}
System.out.println("day : "+day.toString()+ " long : " + day.getTime());
which gives the following output:
day : Thu Feb 28 00:00:00 EET 2013 long : 1362002400000
which is correct but not what I want since the long value results in Wed, 27 Feb 2013 22:00:00 GMT (http://www.epochconverter.com/) (I'm in a GMT+2 timezone). And i need to send to correct long value to sql.
Is there anyway to work around this without using external libraries?
SimpleDateFormat is locale-aware, meaning the date it parses is in your timezone. Midnight 28 Feb in GMT+2 is actually 10pm 27 Feb in GMT, the long value 1362002400000. I would add this to get the parsing right (would't bother using Calendar):
sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
Again, when you print this date it uses SimpleDateFormat and that's why you can see EET in the output.
Passing this to database is a different story though once you get this right.
Use DateFormat.setCalendar(Calendar cal) to set a Calendar with GMT as its timezone, or use DateFormat.setTimeZone(TimeZone zone) with the GMT TimeZone. That will ensure that the resulting Date will be 00:00:00 in GMT instead of in EET.
If you add a timezone specifier to your string you can force java to use GMT for the conversion:
String dayDate = "28-02-2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy z"); // z is a timezone specifier
Date day = new Date();
try {
day = sdf.parse(dayDate + " GMT"); // Use GMT timezone.
} catch (ParseException pe) {
pe.printStackTrace();
}
System.out.println("day : "+day.toString()+ " long : " + day.getTime());
You are converting between text and internal (Date) representations of dates and times without explicitly stating the time-zone. That never goes well.
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
Date date = calendar.getTime();
Use your timezone String:
TimeZones