DayLightSavingMode issue in Android - android

I know this Questions Asked before so many time but i didn't get solution from it.
I am trying to convert Datetime to UTC-0.
I have backend as microsoft sharepoint O365.
I am adding record in sharepoint List.
I am store user timezone when it register. so now whenever user login they can see all data with dateandtime in their selected timezone which store at time of registration.
Now my point is user registered timezone is Asia/Kolkata.
now if he login using other country device like London. then he can see all data date time in Asia/Kolkata which is selected at register time.
For eg. User registered timezone at time of registration like GMT+5:30 then if user mobile timezone is whatever they can see in GMT+5:30 DateTime
And if he add any data from app then it also store as Asia/Kolkata timezone which Datetime not as current mobile timezone if he using Mobile in london.
how to Resolve it?
my code like below:
public String getDateAndTimeToUserFormat()
{
String date;
TimeZone timeZone = TimeZone.getTimeZone("Indian Standard Time");
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat(serverdateFormat, Locale.US);
simpleDateFormat.setTimeZone(timeZone);
date= simpleDateFormat.format(calendar.getTime());
Log.i("TAG","UserTimezone=>"+this.timeZone);
Log.i("TAG","Time zone: " + timeZone.getID());
Log.i("TAG","default time zone: " + TimeZone.getDefault());
Log.i("TAG","default time : " + Calendar.getInstance().getTime());
Log.i("TAG","UTC: " + date);
Log.i("TAG","Default: " + calendar.getTime());
return date;
}
Output:
UserTimezone=>India Standard Time
Time zone: GMT
default time zone: Africa/Casablanca
default time : Fri Jun 17 12:53:55 GMT+00:00 2016
UTC: 2016-06-17T12:53:55Z
Default: Fri Jun 17 12:53:55 GMT+00:00 2016

finally i have make code for my requirement.
public String getDateTimeToUTC() {
String result=convertToUTCDateTime(convertToUserDateTime(this.timeZone));
return result;
}
/**
*
* #param userTimeZone : is contain String Of User Selected TimeZone
* #return Date: it contain DateTime of UserSelected TimeZone
*/
public Date convertToUserDateTime(String userTimeZone)
{ Date parsed = null;
TimeZone timeZone = TimeZone.getTimeZone(userTimeZone);
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat(serverdateFormat, Locale.US);
simpleDateFormat.setTimeZone(timeZone);
String date = simpleDateFormat.format(calendar.getTime());
try {
parsed=simpleDateFormat.parse(date);
Log.i("TAG", "default time zone: " + TimeZone.getDefault().getID());
Log.i("TAG", "User Selected Timezone=>" + timeZone.getID());
Log.i("TAG", "default Device DateTime : " + Calendar.getInstance().getTime());
Log.i("TAG","User selected DateTime=>"+date);
} catch (ParseException e) {
e.printStackTrace();
}
return parsed ;
} /**
* #param userZoneDateTime : is contain User Selected TimeZone Date
* #return String : Return UTC-0 DateTime String From User Selected DateTimeZone
*/
public String convertToUTCDateTime(Date userZoneDateTime)
{
String result;
TimeZone timeZone1 = TimeZone.getTimeZone("UTC");
SimpleDateFormat simpleDateFormat1 =
new SimpleDateFormat(serverdateFormat, Locale.US);
simpleDateFormat1.setTimeZone(timeZone1);
result = simpleDateFormat1.format(userZoneDateTime);
Log.i("TAG", "UTC Time zone: " + timeZone1.getID());
Log.i("TAG", "UTC: " + result);
return result;
}

Related

Setting UTC Timezone is not working in Android

I am trying to convert millisecond time value to UTC 12 hour format using following code:
public void updateDateAndTimeForMumbai(String value) {
SimpleDateFormat outputTimeFormatter = new SimpleDateFormat("h:mm");
SimpleDateFormat outputDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
try {
calendar.setTimeInMillis(Long.parseLong(value));
Log.i("Scheduled date: " + outputDateFormatter.format(calendar.getTime()));
Log.i("Scheduled time: " + outputTimeFormatter.format(calendar.getTime()));
Log.i("Scheduled time Am/Pm: " + new SimpleDateFormat("aa").format(calendar.getTime()));
} catch (NumberFormatException n) {
//do nothing and leave all fields as is
}
}
Here value = "1479633900000"
Output is:
Scheduled date: 20/11/2016
Scheduled time: 2:55
Scheduled time Am/Pm: AM
What I want is:
Scheduled date: 20/11/2016
Scheduled time: 9:25
Scheduled time Am/Pm: AM
I don't know where is the problem.
You need to explicitly use DateFormat.setTimeZone() to print the Date in the desired timezone.
outputDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Call this after you do :
SimpleDateFormat outputDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
If the time you are receiving from server is not UTC time, then you shouldn't set your Calendar instance to UTC. but just directly set your Calendar time.
Remove
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
And call
Calendar calendar = Calendar.getInstance();
Here is how should look your final code :
public void updateDateAndTimeForMumbai(String value) {
SimpleDateFormat outputTimeFormatter = new SimpleDateFormat("h:mm");
outputTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat outputDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
outputDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
try {
calendar.setTimeInMillis(Long.parseLong(value));
Log.i("Scheduled date: " + outputDateFormatter.format(calendar.getTime()));
Log.i("Scheduled time: " + outputTimeFormatter.format(calendar.getTime()));
Log.i("Scheduled time Am/Pm: " + new SimpleDateFormat("aa").format(calendar.getTime()));
} catch (NumberFormatException n) {
//do nothing and leave all fields as is
}
}

Android Date Display Format

I'm learning Android a simple screen with user Information like name,Date of Birth and so on.
All are Text View and Edit Text.
In my pojo Date of Birth datatype is String.
When I enter date and try to get the date it is showing as some long number I this it is date and time in milliseconds.
Can any one help me to solve this.
If i understand your problem.. Try this...
Date d1 = new Date();
d1.setTime(time_in_milliseconds);
Calendar cal = Calendar.getInstance();
cal.setTime(d1);
And then you can get day,month, year..
int number_of_day = cal.get(Calendar.DAY_OF_MONTH)
Check this :
public Date getDateByFormat(String time, String format) {
if (time == null || "".equals(time)) return null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
return sdf.parse(time);
} catch (ParseException e) {
Log.d(Constants.TAG, "Could not parse date: " + time + " Due to: " + e.getMessage());
}
return null;
}
here format is your date string format. Like "yyyy-MM-dd HH:mm:ss"

Local time to EST time Conversion

Android Local time to EST time Conversion
Code:
SimpleDateFormat serverDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
serverDateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
Calendar calender= Calendar.getInstance(Locale.getDefault());
String time=serverDateFormat.format(calender.getTime());
but i getting wrong time.
one hour difference from right time.
for eg :
local time : Tue Jul 07 17:30:00 GMT+05:30 2015
formated time : 2015/07/07 07:00:00
right time : 2015/07/07 08:00:00
Your problem is using the identifier "EST" which stands for "Eastern Standard Time". As the name suggests it does not use daylight saving rules. Proof:
TimeZone tz = TimeZone.getTimeZone("EST");
long offset = tz.getOffset(System.currentTimeMillis()) / (1000 * 3600);
System.out.println(tz.getID() + ":" + tz.useDaylightTime() + "/" + offset);
// output: EST:false/-5
Use the timezone id "America/New_York" instead:
tz = TimeZone.getTimeZone("America/New_York");
offset = tz.getOffset(System.currentTimeMillis()) / (1000 * 3600);
System.out.println(tz.getID() + ":" + tz.useDaylightTime() + "/" + offset);
// output: America/New_York:true/-4
Then you will observe daylight saving time in July making an offset difference of (+05:30) - (-04:00) = +09:30 resulting in the expected local time 8 AM.
hey please try this function for time conversion -
public static String getTime(String time, SimpleDateFormat sdf) {
String convertedTime = "";
try {
TimeZone timeZone = TimeZone.getDefault();
Date postdate = sdf.parse(time);
long postTimeStamp = postdate.getTime() + timeZone.getRawOffset();
String dateString = sdf.format(new Date(postTimeStamp));
convertedTime = dateString;
// convertedTime = getLastTime(context, time);
} catch (Exception e) {
e.printStackTrace();
}
return convertedTime;
}

Android:Display time after adding GMT time zone

I am working on a App in which i want to display notification time.
I can display notification time but not able to add time zone in it.
My current location is Pakistan and i want to add GMT+5:00
My code is attached
String currentDateTimeString = DateFormat.getTimeInstance().format(notif.At);
textViewTime.setText(currentDateTimeString);
in this code, notif.At is dateTime variable. I also attached screenshot of my app, i want to ask you , how to add timeZone value in notif.At. Thanks!
Update
You mark time with timezone in order to solve internationalization problem, I understand, right?
If so, I think it could be better to convert your date to UTC date. When you change to another timezone, just convert this UTC Date to local.
public static Date localToUtc(Date localDate) {
return new Date(localDate.getTime()-TimeZone.getDefault().getOffset(localDate.getTime()));
}
public static Date utcToLocal(Date utcDate) {
return new Date(utcDate.getTime()+TimeZone.getDefault().getOffset(utcDate.getTime()));
}
Old answer
If your notif.At is Dateobject, it's a same question actually:
TimeZone tz = TimeZone.getDefault();
Date date = new Date();
final String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
String result = sdf.format(date);
Log.d("Date ", "date: " + result + " " + tz.getDisplayName(false, TimeZone.SHORT));
print:
date: 2015-03-31 18:45:28 GMT+08:00
You can try java.time api;
Instant date = Instant.ofEpochMilli(1549362600000l);
LocalDateTime utc = LocalDateTime.ofInstant(date, ZoneOffset.UTC);
LocalDateTime pst = LocalDateTime.ofInstant(date, ZoneOffset.of("+05:00"));
LocalDateTime is = LocalDateTime.ofInstant(date, ZoneOffset.of("+05:30"));

Android Timezone Ids are not matching with System Timezone Ids issue

I am working on timezones concept in Android.
I want to change the timezone of the Android tablet by taking the timezone from the App Variable in the application. I am getting the System TimeZones as the variable value i.e like
Dateline Standard Time
UTC-11
Samoa Standard Time
Hawaiian Standard Time
Alaskan Standard Time
Pacific Standard Time (Mexico)
Pacific Standard Time
US Mountain Standard Time
Mountain Standard Time (Mexico)
Mountain Standard Time
Central America Standard Time
Central Standard Time
Central Standard Time (Mexico)
Canada Central Standard Time
SA Pacific Standard Time
From Android case, I am getting Timezone Id's like
Africa/Harare
Africa/Johannesburg
Africa/Kigali
Africa/Lubumbashi
Africa/Lusaka
Africa/Maputo
Africa/Maseru
Africa/Mbabane
Africa/Tripoli
Asia/Amman
Asia/Beirut
Asia/Istanbul
Asia/Jerusalem
Asia/Nicosia
Asia/Tel_Aviv
CAT
EET
Egypt
Etc/GMT-2
Europe/Chisinau
Europe/Helsinki
and my code is
if (mCalendar != null) {
mCalendar = Calendar.getInstance();
}
else
{
String[] allTimeZones = TimeZone.getAvailableIDs();
Arrays.sort(allTimeZones);
for (int i = 0; i < allTimeZones.length; i++) {
System.out.println(allTimeZones[i]);
}
TimeZone tz = TimeZone.getTimeZone(String.valueOf(Jordan Standard Time));
mCalendar = Calendar.getInstance(tz);
String name = tz.getID();
System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " + name);
}
As 'Jordan Standard Time' is variable from application is not like Timezone of tablet available Id's, Timezone is not changing.
If I replace the Timezone with 'Africa/Tripoli' manually, the timezone is replacing with this one.
My issue now is I would like to convert the system timezones to Tablet Timezone Ids and display it in Android Application.
plz use this function that is display GMT Time display if you change timezone from you android phone.
public static String GetDateForGMTDate(String dateString) {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-mm-dd");
SimpleDateFormat formatter1 = new SimpleDateFormat(
"yyyy-mm-dd");
Date date = null;
try {
date = formatter.parse(dateString);
System.out.println(date);
System.out.println(formatter.format(date));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Log.i("Time zone", "gettime=" + cal.getTime());
cal.add(Calendar.MINUTE, (-1 * getTimeZoneDifference()));
Log.i("Time zone", "after gmt +gettime=" + cal.getTime());
date = cal.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return formatter1.format(date);
}
public static final int getTimeZoneDifference() {
long currentTime = System.currentTimeMillis();
int gmtcurrentOffset = TimeZone.getDefault().getOffset(currentTime);
int gmtOffset = TimeZone.getTimeZone("GMT").getOffset(currentTime);
int minuteDifference = (((gmtOffset - gmtcurrentOffset) / 1000) / 60);
return minuteDifference;
}
use 1st function and pass any date with yyy-mm-dd formate as a string. and that function will return gmt formate real date-time.
i already use it.
its working fine.i hope its useful to you.

Categories

Resources