美文网首页
时间转成毫秒数

时间转成毫秒数

作者: 胖子爱猪蹄 | 来源:发表于2017-12-14 16:22 被阅读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

    相关文章

      网友评论

          本文标题:时间转成毫秒数

          本文链接:https://www.haomeiwen.com/subject/epvkwxtx.html