admin 管理员组文章数量: 1184232
/**
* @param strTime 示例:"2020-01-01 23:59:59"
* @return "2020-01-01T15:59:59Z"
*/
//传入一个北京时间(字符串), 返回ISO-8601时间(字符串)格式出去.
public static String getISOTime(String strTime) {
try {
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(strTime);
//把时间转换成ISO-8601格式, 需要减去8个小时
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, -8);
Date newTime = calendar.getTime();
String isoTime = DateFormatUtils.format(newTime, pattern);
return isoTime;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
//直接传入一个时间格式, 返回ISO-8601时间(字符串)格式出去.
public static String getISOTimeByDate(Date date) {
//把时间转换成ISO-8601格式, 需要减去8个小时
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, -8);
return DateFormatUtils.format(calendar.getTime(), pattern);
}
/**
* @param isoTime 示例:"2021-01-19T16:00:00Z"
* @return "2021-01-20 00:00:00"
*/
//传入一个ISO-8601时间格式, 返回中国标准时间(字符串)格式出去
public static String getTimeByISO(String isoTime) {
try {
Date isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(isoTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(isoDate);
calendar.add(Calendar.HOUR, 8);//ISO-8601时间格式的转成中国标准时间格式的,需要加上8个小时.
Date newTime = calendar.getTime();
String strTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newTime);
return strTime;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
//传入一个ISO-8601时间格式, 返回中国标准时间格式出去
public static Date getDateByISO(String isoTime) {
try {
Date isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(isoTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(isoDate);
calendar.add(Calendar.HOUR, 8);//ISO-8601时间格式的转成中国标准时间格式的,需要加上8个小时.
Date newTime = calendar.getTime();
return newTime;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}版权声明:本文标题:学会这个小技巧,轻松将ISO时间格式变为普通时间! 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1771794010a3548628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论