我试图在将字符串“2018-10-17T22:00:00Z”作为事件添加到日历时将其转换为东部时区,但不是将事件时间添加为下午 6 点,而是将其添加为晚上 10 点。任何帮助,将不胜感激。
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));
相关分类