天天看點

微信公衆帳号開發-消息建立時間long型與标準時間的互相轉換

/**

 * 

 */

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 

     * @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;  

}  若轉載請注明出處!若有疑問,請回複交流!