将String日期时间格式转换及截断

来源:互联网 发布:js接口安全域名 备案 编辑:程序博客网 时间:2024/05/29 13:46

记录一下:

数据传输过来时日期时间类型如:2016-02-22 18:30:38需要转换成2016-02-22 18:30

换不多说,直接上代码:

public static String getTime(String user_time) {String re_time = null;SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date d;try {d = sdf.parse(user_time);long l = d.getTime();String str = String.valueOf(l);re_time = str.substring(0, 10);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return getStrTime(re_time);}public static String getStrTime(String cc_time) {String re_StrTime = null;SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 例如:cc_time=1291778220long lcc_time = Long.valueOf(cc_time);re_StrTime = sdf.format(new Date(lcc_time * 1000L));return re_StrTime;}


0 0
原创粉丝点击