Android Studio将ISO字符串转换为“America / New_York”将事件添加到日历时 [英] Android Studio Convert ISO string to "America/New_York" when adding to event to calendar
问题描述
我正在尝试将字符串2018-10-17T22:00:00Z转换为东部时区,将其作为事件添加到日历中,但不是将事件时间添加为下午6点,而是将其添加为下午10点。任何帮助将不胜感激。
I am trying to convert a string "2018-10-17T22:00:00Z" to eastern time zone when adding it as an event to a calendar, but instead of it adding the event time as 6pm it is adding it as 10pm. Any help would be appreciated.
String[] segments = uri.getPath().split("/");
Date startDate = null;
Date endDate = null;
Activity activity = (Activity) context;
String stTime = "2018-10-17T22:00:00Z";
String enTime = "2018-10-17T23:00:00Z";
String eventTitle = segments[3];
String eventLocation = segments[4];
TimeZone timezone = TimeZone.getTimeZone("America/New_York");
SimpleDateFormat formatStart = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
formatStart.setTimeZone(timezone);
SimpleDateFormat formatEnd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
formatEnd.setTimeZone(timezone);
try {
startDate = formatStart.parse(stTime);
endDate = formatEnd.parse(enTime);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calStart = new GregorianCalendar(timezone);
calStart.setTime(startDate);
Calendar calEnd = new GregorianCalendar(timezone);
calEnd.setTime(endDate);
if (ActivityCompat.checkSelfPermission(this.context, android.Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.WRITE_CALENDAR},1);
}
Calendar beginTime = Calendar.getInstance();
beginTime.set(calStart.get(Calendar.YEAR), calStart.get(Calendar.MONTH), calStart.get(Calendar.DAY_OF_MONTH), calStart.get(Calendar.HOUR_OF_DAY), calStart.get(Calendar.MINUTE));
Calendar endTime = Calendar.getInstance();
endTime.set(calEnd.get(Calendar.YEAR), calEnd.get(Calendar.MONTH), calEnd.get(Calendar.DAY_OF_MONTH), calEnd.get(Calendar.HOUR_OF_DAY), calEnd.get(Calendar.MINUTE));
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
values.put(CalendarContract.Events.TITLE, eventTitle);
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "America/New_York");
values.put(CalendarContract.Events.EVENT_LOCATION, eventLocation);
cr.insert(CalendarContract.Events.CONTENT_URI, values);
Toast.makeText(context, "Successfully Added Event", Toast.LENGTH_SHORT).show();
推荐答案
那是因为你告诉解析器忽略 Z
,或者更确切地说是字面匹配,但不处理它的意思。
That is because you're telling the parser to ignore the Z
, or rather to match it literally, but not to process it's meaning.
输入字符串是 Instant
值,因此解析为 Instant
,然后应用时间如果您没有兼容Java 8+的Android,请使用 ThreeTenABP 。
The input strings are Instant
values, so parse to Instant
, then apply time zone.
If you don't have Java 8+ compatible Android, use ThreeTenABP.
String stTime = "2018-10-17T22:00:00Z";
ZonedDateTime time = Instant.parse(stTime).atZone(ZoneId.of("America/New_York"));
System.out.println(time); // prints: 2018-10-17T18:00-04:00[America/New_York]
如果你喜欢使用过时的 SimpleDateFormat
,只需使用正确的格式,而不是指定时区。
If you prefer using antiquated SimpleDateFormat
, simply use the correct format, and don't specify time zone.
SimpleDateFormat formatStart = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date startDate = formatStart.parse(stTime);
// My default time zone is America/New_York, so:
System.out.println(startDate); // prints: Wed Oct 17 18:00:00 EDT 2018
startDate .getTime()
是你需要的时间。
对于API等级< 24,格式模式 X
不起作用,所以你需要在你的问题中做硬编码的 Z
,但是告诉解析器它是UTC格式,因为这是 Z
时区的含义。
For API Level < 24, format pattern X
doesn't work, so you need to do the hardcoded Z
like in your question, but tell the parser that it is in UTC, because that is what the Z
time zone means.
SimpleDateFormat formatStart = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
formatStart.setTimeZone(TimeZone.getTimeZone("UTC"));
Date startDate = formatStart.parse(stTime);
// My default time zone is America/New_York, so:
System.out.println(startDate); // prints: Wed Oct 17 18:00:00 EDT 2018
这篇关于Android Studio将ISO字符串转换为“America / New_York”将事件添加到日历时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!