时间转成毫秒数

2017-12-14  本文已影响0人  胖子爱猪蹄

日期时间字符串和毫秒之间的相互转换

package com.guoyasoft;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;

//日期时间字符串转换成毫秒
//毫秒范围0-1000
public class calendar {
    @Before
    public void test() throws ParseException {
        String dateTime = "2017-12-14 15:50:45 123";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").parse(dateTime));
        System.out.println("日期[2017-12-14 15:50:45 123]对应毫秒:" + calendar.getTimeInMillis());
    }
    @Test
    public void test2() {
        long millisecond = 1513237845123l;
        Date date = new Date(millisecond);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss SSS a");
        System.out.println("毫秒[1513237845123]对应日期时间字符串:" + format.format(date));
    }
}

image.png
上一篇下一篇

猜你喜欢

热点阅读