Android SimpleDateFormat not recognizing daylight time - android

I am trying to parse a string of time given in GMT to a new string with the user's timezone. SimpleDateFormat is parsing the time as PST while it should be PDT. Printing the current time provides the correct timezone. I have a feeling the problem might be with using a date, but I am unsure what to do.
Sample code:
try {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a z", Locale.ENGLISH);
Log.e("Current time", sdf.format(Calendar.getInstance().getTime()));
sdf.applyPattern("HHmm z");
Date date = sdf.parse("2137 GMT");
sdf.applyPattern("hh:mm a z");
Log.e("Parsed time", sdf.format(date));
}catch (Exception e){ e.printStackTrace(); }
The two logs produce:
E/Current time﹕ 02:37 PM PDT
E/Parsed time﹕ 01:37 PM PST
Any help would be greatly appreciated.

I was finally able to figure out the issue. By default a date object is set for Jan 1, 1970. Daylight savings time does not apply during January so it wasn't using it. What I did was add the day of the year to the pattern and then get the current date from the calendar.
Updated portion:
sdf.applyPattern("D HHmm z");
Date date = sdf.parse(sdf.getCalendar().get(Calendar.DAY_OF_YEAR)+" 0420 GMT");
This correctly adjust for daylight savings time.

The date that's in used is during daylight savings time

Related

How can i convert my GMT time into IST of 12 hour format in android?

Explanation:
I have time in GMT format i need to convert into IST. I did already but for 24 hour i want to get in 12 hour format with AM/PM.
here is my code
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
utcFormat.setTimeZone(TimeZone.getTimeZone("IST"));
Date date1;
date1=utcFormat.parse(time);
Log.e("IST",""+date1.toString());//After convert in IST i got Sat Mar 26 19:30:00 GMT+05:30 2016
Date timestamp;
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
timestamp = utcFormat.parse(time);
Log.e("timestamp", "" + timestamp.toString()); //After convert in UTC i got Sat Mar 26 19:30:00 GMT+05:30 2016
Log.e("Time",""+time.toString());//This is my time 2016-03-26T14:00+00:00
Problem is i got the same time for IST and GMT.
What will i need to do so than i will get a time in 12 hours???
Please help me to solve out this problem.
Remember that the Date class does not hold any actual timezone information, it is just a long millisecond value from UNIX epoch time. And the TimeZone of the SimpleDateFormat is only relevant when you display the date, not when you parse it. So in order to print your Date as an IST date, you must use that format when printing the Date:
// Parse your time string
DateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
Date date = myFormat.parse(time);
// Set a new format for displaying your time
DateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
isoFormat.setTimeZone(TimeZone.getTimeZone("IST"));
// This will print the IST time in the format specified: 2016-03-25T09:42:07+5:30
String istTime = isoFormat.format(date);
Log.d(istTime);
As for the 12 hour format, you can set that with a new SimpleDateFormat when you print the time.
/**
* 'hh' is the 12-hour time format, while 'HH' is the 24-hour format
* 'hh' will always print two digits, while 'h' will drop leading zeros
* 'a' is the AM/PM marker
*/
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
// This will print the current time, for example: 09:42 AM
Log.d(dateFormat.format(new Date()));
Android also has a helpful DateUtils class, that will format the Date based on the settings of the current device.
Here i was created a another object of DateFormat and pass a parameter inside it' constructor.
//hh:mm a format of 12 hour with leading 0 before the hours, mm is for minutes and (a) is for AM/PM format.
//where as HH is provides the format of 24 hours.
Here i got a 12 hours format.
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm'+00':ss");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
DateFormat outputFormat = new SimpleDateFormat("hh:mm a");
String ist=outputFormat.format(utcFormat.parse(time));

Simple date formatting giving wrong time

I am trying to parse a String to a Date and it giving me right date where as time is wrong.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
try {
Date date = formatter.parse("2015-08-20 05:00 AM");
Log.e("date", date+""); // Logcat printing Thu Aug 20 00:00:00 GMT+05:30 2015
} catch (ParseException e) {
Log.e("Error",e.toString());
}
As you can see irrespective of the time every time parsed date time showing 00:00:00
What I want is Thu Aug 20 05:00:00 GMT+05:30 2015
It seems the problem was that your pattern String specified am/pm, but was using uppercase H's for the hour characters. These indicate a 24-hour clock, which obviously doesn't use am/pm. Change the hour characters to lowercase h's, which indicate the hour in am/pm (0-11).
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
The docs for SimpleDateFormat explain the various acceptable pattern characters.
Try below code
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");
String dateToStr = format.format(new Date());
System.out.println("dateToStr=>" + dateToStr);
try {
Date strToDate = format.parse(dateToStr);
System.out.println("strToDate=>" + strToDate);
} catch (ParseException e) {
e.printStackTrace();
}
Maybe its related to time zone issues, at what time zone is your input?, i'd suggest to make sure your paramater is on UTC timezone and then using formatter like this :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm aaa");
formatter .setTimeZone(TimeZone.getTimeZone("UTC"));
Try this code you missed some lines of code
SimpleDateFormat mFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aa", Locale.getDefault());
mFormatter.setTimeZone(TimeZone.getTimeZone("India"));
Date startDate = mFormatter.parse("2015-08-20 05:00 AM");
This code working fine for me.

formatting date in android

I'm trying to display date which I already have to a new format, i'm using SimpleDateFormat for this.
Android Code
String date = "2013-08-11 20:38 EDT";
SimpleDateFormat sf = new SimpleDateFormat("dd MMMM yyyy hh:mm a");
try {
newDate = sf.format(new SimpleDateFormat("yyyy-MM-dd kk:mm z").parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
It displays :
11 August 2013 08:38 PM
However if I run the same code in JAVA (as a normal JAVA console application)
JAVA
String date = "2013-08-11 20:38 EDT";
SimpleDateFormat sf = new SimpleDateFormat("dd MMMM yyyy hh:mm a");
String lDate = sf.format(new SimpleDateFormat("yyyy-MM-dd kk:mm z").parse(date));
System.out.println(lDate);
It displays :
12 August 2013 06:08 AM
This is the format which I need to display.
P.S. : I got a warning in android saying
To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.
so I tried adding Locale.US to SimpleDateFormat it again show the 11 August 2013 08:38 PM and not 12 August 2013 06:08 AM
My Question is :
how to display date as 12 August 2013 06:08 AM in android.
Your local time IST is UTC+05:30, your Android time EDT is UTC-04:00. Together they add to 9h30min of difference explaining the difference in output.
Set your Android device to IST timezone to get the same output.
Alternatively, you can call setTimeZone() on the DateFormat to explicitly set a timezone to use.
It is also helpful to explicitly print timezone information to make datetime stamps less ambiguous.
new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.S", Locale.US)
You should specify the time zone
try this code
SimpleDateFormat simple = new SimpleDateFormat("dd MMMM yyyy hh:mm a" , java.util.Locale.getDefault());

convert date time from one timezone to another in android

I am using smack api for instance messages.I want to send the current time with the messages.So i have set the current time as message subject.and then get that time on receiver side.But the problem is that message subject should be string so i have convert the date time to string at sender side and then again convert from string to date time on receiver side.I want the sender's date time should be convert as per receiver's Timezone.I have wrote the code as below but i can't convert the date time to receiver's Timezone's date time.
//sender side
Calendar c = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
String strdt= formatter.format(c.getTime());
message.setSubject(strdt);
receiver side
TimeZone tz = TimeZone.getDefault();
String strzone=tz.getDisplayName(false, TimeZone.SHORT);
String str=message.getSubject();
Date expiry = null;
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
formatter.setTimeZone(TimeZone.getTimeZone(strzone));
try {
expiry = formatter.parse(str);
}
catch (Exception e)
{e.printStackTrace();}
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
df.setTimeZone(TimeZone.getTimeZone(strzone));
String formattedDate1= df.format(expiry);
Log.i("receiving time",formattedDate1);
i got one output like
message subject=> Wed Aug 22 13:35:13 GMT+00:00 2012
after convert from string to date => Wed Aug 22 07:35:13 MDT 2012
after set the time zone of receiver => Wed Aug 22 06:35:13 GMT-07:00 2012
actual receiver side time => Wed Aug 22 06:39:56 MDT 2012
Edit
Actually i want to send the sender's time to receiver and on receiver side the should be convert as per receiver's time zone.as describe above the receiver's actual time is different than the converting time.So please if you have different code then please post here.
Use plain longs for time. (Long.toString(new Date().getTime()). It does not contain (nor need) any TZ info, and is therefore more flexible.

Formatting a string timestamp with Android

For some reason this has me tearing my hair out.
I have a UNIX timestamp as a string in Android. All I want to do is format this so that it returns the date/time in the user's droid time zone.
I can convert it to a timestamp just fine, but it uses GMT rather than their localised zone.
Thanks
Use the SimpleDateFormat constructor with the Locale you need:
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat%28java.lang.String,%20java.util.Locale%29
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
Calendar c = Calendar.getInstance();
try {
Date dt = sdf.parse("2011-03-01 17:55:15");
c.setTime(dt);
System.out.println( c.getTimeInMillis());
System.out.println(dt.toString());
} catch (ParseException e) {
System.err.println("There's an error in the Date!");
}
outputs:
1299002115000
Tue Mar 01 12:55:15 EST 2011

Categories

Resources