微信公众帐号开发-消息创建时间long型与标准时间的互相转换
时间:2016-12-30 18:10:36
收藏:0
阅读:310
/**
*
*/
package com.hd.admin.wxmeet.utils;
/**
* @author jymcpp
*
*/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class wxLongutils {
public static void main(String[] args){
String createTime = "1483084803";
String formatTime = formatTime(createTime);
System.out.println(formatTime);
String year = formatTime.substring(6, 10);
String month = formatTime.substring(0, 2);
String day = formatTime.substring(3, 5);
String hour = formatTime.substring(11, 13);
String minute = formatTime.substring(14, 16);
String second = formatTime.substring(17, 19);
System.out.println(year);
System.out.println(Long.parseLong(month));
System.out.println(day);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
String sDt = "08/07/2014 10:14:36";
System.out.println(formatToLongTime(sDt));
}
/**
* 将微信消息中的CreateTime转换成标准格式的时间(MM/dd/yyyy HH:mm:ss)
*
* @param createTime 消息创建时间
* @return
*/
public static String formatTime(String createTime) {
// 将微信传入的CreateTime转换成long类型,再乘以1000
long msgCreateTime = Long.parseLong(createTime) * 1000L;
DateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
return format.format(new Date(msgCreateTime));
}
/**
* 将标准格式的时间(MM/dd/yyyy HH:mm:ss)转换为微信服务器的long型
* @param formatTime
* @return
* @throws ParseException
*/
public static long formatToLongTime(String formatTime){
SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date dt2;
try {
dt2 = sdf.parse(formatTime);
return dt2.getTime() / 1000;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//转换失败返回值
return -1;
}
}
评论(0)