java日历添加事件_java – Android Studio将事件添加到日历时将...

我正在尝试将字符串“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));

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,或者更确切地说是字面匹配,而不是处理它的意思.

输入字符串是Instant值,因此解析为Instant,然后应用时区.

如果您没有兼容Java 8的Android,请使用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,只需使用正确的格式,不要指定时区.

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时区的意思.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值