far*_*ath 13 java android calendar google-calendar-api simpledateformat
我需要将日期格式化为特定的字符串.
我使用SimpleDateFormat类来格式化日期,使用模式" yyyy-MM-dd'T'HH:mm:ssZ"它将当前日期返回为
" 2013-01-04T15:51:45+0530",但我需要为
" 2013-01-04T15:51:45+05:30".
以下是使用的编码,
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);
Log.e(C.TAG, "formatted string: "+sdf.format(c.getTime()));
Run Code Online (Sandbox Code Playgroud)
输出:格式化字符串:2013-01-04T15:51:45+0530
我需要格式2013-01-04T15:51:45+05:30只是在gmt时间之间添加冒号.
因为我正在使用Google日历来插入活动,所以它只接受我提到的所需格式.
Gri*_*mmy 29
您也可以在模式中使用"ZZZZZ"而不是"Z"(根据文档).像这样的东西
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.ENGLISH);
Log.e(C.TAG, "formatted string: "+sdf.format(c.getTime()));
Run Code Online (Sandbox Code Playgroud)
fge*_*fge 13
您可以改用Joda Time.它DateTimeFormat有一个ZZ格式属性,可以满足你的需要.
大优势:不像SimpleDateFormat,DateTimeFormatter是线程安全的.用法:
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ")
.withLocale(Locale.ENGLISH);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20853 次 |
| 最近记录: |