Tôi có thời gian bằng mili giây và tôi hiển thị thời gian đó trong đơn đăng ký của mình ở định dạng "MM/dd/yyyy HH:mm:ss"
, nó hoạt động tốt.Sự cố múi giờ trong Android
Sự cố: nếu tôi thay đổi múi giờ trong thiết bị Android, thời gian hiển thị trong ứng dụng của tôi có thể là do tính toán múi giờ. Tôi muốn hiển thị cùng một thời điểm trong cơ sở dữ liệu của tôi vì nó nằm trong bất kỳ múi giờ nào.
Tôi đang sử dụng mã dưới đây để kiểm tra vấn đề múi giờ
Locale locale = new Locale("en", "IN");
TimeZone tz = TimeZone.getDefault();
System.out.println("Default timezon id :: " + tz.getID());
tz.setID("Asia/Calcutta");
String timezon = tz.getDisplayName(false, TimeZone.SHORT, locale);
System.out.println("Timezon id :: " + tz.getID() + " Timezon name :: " + timezon);
Calendar cal = Calendar.getInstance(tz, locale);
System.out.println("TImezon :: " + tz);
// "1319084400775" is the milliseconds of 10/20/2011 05:20:00 in MM/dd/yyyy HH:mm:ss format
Date date = new Date(Long.parseLong("1319084400775"));
System.out.println("Date :: " + date.toGMTString());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", locale);
System.out.println("Start date :: " + format.format(date));
// "1319084400775" is the milliseconds of 10/20/2011 05:20:00 in MM/dd/yyyy HH:mm:ss format
cal.setTimeInMillis(Long.parseLong("1319084400775"));
timezon = cal.getTimeZone().getDisplayName(false, TimeZone.SHORT, locale);
System.out.println("Calender's timezon :: " + timezon);
System.out.println("Start date :: yyyy-mm-dd hh:mm:ss " + cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));
// "1319084700776" is the milliseconds of 10/20/2011 05:25:00 in MM/dd/yyyy HH:mm:ss format
cal.setTimeInMillis(Long.parseLong("1319084700776"));
System.out.println("End date :: " + cal.getTime());
Output ::
Nếu tôi đặt múi giờ cho "Thái Bình Dương/Fiji";
10-07 17:03:40.392: INFO/System.out(1193): Default timezon id :: Pacific/Fiji
10-07 17:03:40.392: INFO/System.out(1193): Timezon id :: Asia/Calcutta Timezon name :: GMT+05:30
10-07 17:03:40.406: INFO/System.out(1193): TImezon :: [email protected]
10-07 17:03:40.422: INFO/System.out(1193): Date :: 20 Oct 2011 04:20:00 GMT
10-07 17:03:40.442: INFO/System.out(1193): Start date :: 2011-10-20 16:20:00
10-07 17:03:40.442: INFO/System.out(1193): Calender's timezon :: GMT+05:30
10-07 17:03:40.442: INFO/System.out(1193): Start date :: yyyy-mm-dd hh:mm:ss 2011-9-20 16:20
10-07 17:03:40.452: INFO/System.out(1193): End date :: Thu Oct 20 16:25:00 Pacific/Fiji 2011
Nếu tôi đặt múi giờ cho "Mỹ/Tijuana";
10-06 22:05:20.702: INFO/System.out(1193): Default timezon id :: America/Tijuana
10-06 22:05:20.712: INFO/System.out(1193): Timezon id :: Asia/Calcutta Timezon name :: GMT+05:30
10-06 22:05:20.712: INFO/System.out(1193): TImezon :: [email protected]
10-06 22:05:20.733: INFO/System.out(1193): Date :: 20 Oct 2011 04:20:00 GMT
10-06 22:05:20.742: INFO/System.out(1193): Start date :: 2011-10-19 21:20:00
10-06 22:05:20.742: INFO/System.out(1193): Calender's timezon :: GMT+05:30
10-06 22:05:20.752: INFO/System.out(1193): Start date :: yyyy-mm-dd hh:mm:ss 2011-9-19 21:20
10-06 22:05:20.752: INFO/System.out(1193): End date :: Wed Oct 19 21:25:00 America/Tijuana 2011
EDIT
tôi có thời gian trong mili giây bây giờ tôi đang hiển thị rằng mili giây ở định dạng ngày tháng trong ứng dụng của tôi, nhưng vấn đề là khi tôi đã thay đổi múi giờ trên điện thoại tôi nhận được ngày theo múi giờ đó.
bạn có thể thêm (cal.get (Calendar.MONTH) +1) vào dòng in không?và vẫn còn bối rối truy vấn của bạn là gì? – jazz
@Shahzeb Đây là gì? – Dharmendra
@jazz Bạn có thể thấy đầu ra khi tôi thay đổi đầu ra timezon khác nhau mà tôi không muốn có nghĩa là tôi muốn ngày và giờ giống như tôi nhập vào số – Dharmendra