Convert GMT+ date to local on android - android

I need to convert date like Mon Nov 14 13:36:01 GMT+03:00 2016 to local. So it must be 16:36:01
Date serverDate; // Mon Nov 14 13:36:01 GMT+03:00 2016
SimpleDateFormat dateFormat = new SimpleDateFOrmat("hh:mm:ss");
String result = dateFormat.formatDate(serverDate);
Result doesn't see GMT +03:00 and gives me wrong time (13:36 instead of 16:36).

So I just needed to create formatter without ZZZZZ and set time zone to GMT.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
final Date localDate = simpleDateFormat.parse("2016-11-11T09:48:24-05:00");
SimpleDateFormat dateFormat = new SimpleDateFOrmat("hh:mm:ss");
String result = dateFormat.formatDate(localDate); //12:48:24
After this code time of localDate variable will be 12:48:24 (in case of my GMT +03:00).

Related

Parsing datetime to UTC

I have this date => 2017-10-17 10:23:30 UTC TIME
I'm parsing it like this
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
TimeZone tz = TimeZone.getDefault(); // +03:00
dateFormat.setTimeZone(tz);
Date parsed_date = dateFormat.parse("2017-10-17 10:23:30");
but this gives me this
Tue Oct 17 13:23:30 GMT+03:00 2017
I want this
2017-10-17 01:23:30
What's the problem?
Thank you guys for your help that's what i wonted i turn out that if the date in UTC you should start with parsing in the utc format that will keep the date as its the for formating it to your local timezone format it with local time zone
private String getDate(String dateString) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date value = null;
try {
value = formatter.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm aa");
dateFormatter.setTimeZone(TimeZone.getDefault());
String dt = dateFormatter.format(value);
return dt;
}
A Date does not have a time zone. So your parsing works since Tue Oct 17 13:23:30 GMT+03:00 2017 is the same point in time as 2017-10-17 10:23:30 UTC.
The time zone is only added when you try to print a date. For example:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed_date= dateFormat.parse("2017-10-17 10:23:30");
String strDate = dateFormat.format(parsed_date);
System.out.println(strDate);
prints:
2017-10-17 10:23:30
Update: to show a date in another format / time-zone you can use something like this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed_date= dateFormat.parse("2017-10-17 10:23:30");
SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
printFormat.setTimeZone(TimeZone.getTimeZone("GMT+3"));
String strDate = printFormat.format(parsed_date);
System.out.println(strDate);
This prints:
2017-10-17 01:23:30

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.

How to convert time in 12 hr format?

I'm getting call time through a cursor and want to display it in 12 hr format instead of 24 hr format.
Here is my code to get time from a cursor
String callDate = managedCursor.getString(dateIndex);
Date callDayTime = new Date(Long.valueOf(callDate));
and set this call day time to text view by
lastintreactionvalueTV.setText(callDayTime+"");
it's showing it like
Thu Jun 26 14:36:24 EDT 2014
What should I do to convert it into 12 hr format?
Use SimpleDateFormat to set the format that you need, for example:
Date callDayTime = new Date();
DateFormat sdf= new SimpleDateFormat("EEE, MMM dd KK:mm:ss a z yyyy",new Locale("en"));
System.out.println(sdf.format(callDayTime) );
This will output:
Thu, Jun 26 08:54:51 AM CEST 2014
Use this,
SimpleDateFormat read = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat write = new SimpleDateFormat("MMMM-dd-yy");
String op = write.format(read.parse("2014-05-17 15:45:56"));
//
SimpleDateFormat read = new SimpleDateFormat("input format");
SimpleDateFormat write = new SimpleDateFormat("output format");
String op = write.format(read.parse("your date as in input format"));

How to get standard format date in android

if i try the below code for eg:
i will set the date as Fri July 25 2014 10:00 AM. which gives date in milliseconds as 1402080056000,
now if i try to read the same milliseconds to date as below
long time = 1402080056000;
Date mydate = new Date(time);
mydate variable shows date as Sat Jun 25 00:10:56 IST 2014
String DateTimeString = DateFormat.getDateTimeInstance().format(new Date(time));
with the above statement in DateTimeString i get date as Jun 25 , 2014 12:10:56 AM
How to read the datetime present in 1402080056000 to Fri July 25 2014 10:00 AM
Just need to work on the format string,
String dateTimeString=
String.valueOf(new SimpleDateFormat("dd-MM-yyyy hh:mm").format(new Date(time)));
Explicitly set time zone:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String result = String.valueOf(dateFormat.format(millis));
Also, this would be useful Regarding Timezones and Java
try below code:
private String convertMilliToDate(long timestamp) {
DateFormat formatter = new SimpleDateFormat("YOUR DATE FORMAT");
// Create a calendar object that will convert the date and time value in
// milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return formatter.format(calendar.getTime());
}
Try this:
String date= DateFormat.format("dd/MM/yyyy hh:mm:ss", new Date(date_in_milis)).toString();
Date-time work is easier using the Joda-Time library, which works on Android.
long millis = 1402080056000L;
DateTimeZone timeZoneIndia = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeIndia = new DateTime( millis, timeZoneIndia );
DateTime dateTimeFrance = dateTimeIndia.withZone( DateTimeZone.forID( "Europe/Paris" ) );
DateTime dateTimeUtc = dateTimeIndia.withZone( DateTimeZone.UTC );
To parse a string as a date-time, search StackOverflow for "Joda parse" to find many examples.

convert a date string into long in Android

I have a date string: 02/28/2013 06:20:00 PM
I have a date formater: SimpleDateFormat dateFmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
java.util.Date tempDate;
when I parse the string with the formater and extract the date
tempDate = dateFmt.parse(xpp.getText());
I get a date that is off by 12 hours: Thu, Feb 28, 2013 6:20 AM
What am i overlooking?
The javadocs for SimpleDateFormat say that HH is for the hour in 24-hour format (0-23). For AM/PM format (1-12) you should use hh.

Categories

Resources