Reputation: 17383
i am using this function to get the current time on the real device(tested on Galaxy S4):
private String getCurrentTime(){
TimeZone mtz = TimeZone.getTimeZone(TimeZone.getDefault().toString());
Calendar calendar = Calendar.getInstance(mtz);
String current_time = calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
return current_time ;
}
but real time (for example) :
18:48:16
The return value of the function:
14:17:41
Automatic date & time and Automatic time zone on the phone has been checked !!
but this function works :
private String getCurrentTime(){
TimeZone mtz = TimeZone.getTimeZone("GMT+04:30");
Calendar calendar = Calendar.getInstance(mtz);
String current_time = calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
return current_time ;
}
Another question:
When I change the time on the phone, The return value of 2 previous function changes, How can I prevent this? In other words, How can I get real time (Without any internet connection)?
Upvotes: 0
Views: 431
Reputation: 14348
In first sample you receive time in GMT because of toString conversion. To obtain your device timezone, just use:
TimeZone mtz = TimeZone.getDefault();
Upvotes: 1