问题
I want to create an alarm application for FIFA 2014 world cup matches in which i have a server which stores the date and time for the matches in Brazil/Acre and my client is an android application and an android device may have any of the possible timezone so the my problem is i want to convert the Brazil/Acre timing to the local android device timing with different timezone and after lot of googled i came to know about joda data and time lib but it is too slow in android so please suggest any code that will work for me.
回答1:
In my opinion Time class is the best for your job. Also it is Android API not general Java API.
Here I mentioned some of useful methods for your job:
void switchTimezone(String timezone)
Convert this time object so the time represented remains the same, but is instead located in a different timezone.
static String getCurrentTimezone()
Returns the timezone string that is currently set for the device.
And if you want to save a time in a timezone independed manner, you can convert to milliseconds (in UTC) by toMillis()
method and then retrieve it by set(long millis)
method.
If something is unclear please tell me!
UPDATE
Example:
long timeMillis = /* get time milliseconds form the server */
Time time = new Time();
time.set(timeMillis);
/* changing time zone */
time.switchTimezone(/* your desired timezone in string format */);
/* getting time as string */
String timeString = time.format("%Y%m%dT%H%M%S"); // you can change format as you wish
Here is a table for formatting times
回答2:
You could use this code, which substracts the hour-difference between Brazil and the local timezone. Just replace yourDate
with a Date-object.
//code...
yourDate.setTime(yourDate.getTime() - getDifferenceInMillis());
//code...
public int getDifferenceInMillis() {
// Local Time
int localMinute = Calendar.getInstance().get(Calendar.MINUTE);
int localHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int localDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
// Brazil Time
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("Brazil/Acre"));
c.setTimeInMillis(new Date().getTime());
int brazilMinute = c.get(Calendar.MINUTE);
int brazilHour = c.get(Calendar.HOUR_OF_DAY);
int brazilDay = c.get(Calendar.DAY_OF_MONTH);
// Difference between Brazil and local
int minuteDifference = brazilMinute - localMinute;
int hourDifference = brazilHour - localHour;
int dayDifference = brazilDay - localDay;
if (dayDifference != 0) {
hourDifference = hourDifference + 24;
}
return (hourDifference * 60 + minuteDifference) * 60 * 1000;
}
回答3:
You should store your date has a long or timestamp in your server. If you don't want you can anyway send your date as a long generate from your database date. Then create your calendar instance like that :
Calendar c = Calendar.getInstance();
c.setTime(new Date(yourdatelong));
You can have to multiplie and add some constant in your long. In java the definition is "the number of milisecond since 1970 1/1 00:00:00". It is differents in C#, for exemple ( number of nanoseconde from 1/1/1900, if I remenber well).
Like that you are sure to set the same date in all your device. When it is done, you just have to put the timezone that you want in your calendar to display the local time.
http://developer.android.com/reference/java/util/Calendar.html
You can have many option to manage time and display it in this class.
回答4:
If dates are stored in server time zone (Brazil/Acre), you should load date time from DB, convert it to UTC time zone and send to client. On client side change UTC to local time zone:
Server side:
DateTime dateOnServer = // load date from db
DateTime dateUTC = dateOnServer.withZone(DateTimeZone.UTC); // convert to UTC
String dateAsStringUTC = dateUTC.toString("yyyy-MM-ddTHH:mm:ss");
// send 'dateAsStringUTC' to client
Client side:
String dateAsStringUTC = // receive date from server
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-ddTHH:mm:ss"); // parser for date
DateTime dateOnClient= dtf.parseDateTime(dateAsStringUTC);
// 'dateOnClient' will be in client time zone 'DateTimeZone.getDefault()'
回答5:
I faced same problem like you...
I got the solution using SimpleDateFormat
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = format.parse("2011-03-01 15:10:37"); // => Date is in UTC now
TimeZone tz = TimeZone.getTimeZone("America/Chicago");
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
destFormat.setTimeZone(tz);
String result = destFormat.format(parsed);
this may help you..
来源:https://stackoverflow.com/questions/23968830/how-to-get-time-with-respect-to-another-timezone-in-android