原老师的时间转换问题 (掉渣天)

//包 

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

// timeStamp时间戳和SimpleDateFormat转换日期格式
        @Test
        public void timeStampTest() {
            
            //01:日期转换成string
            Date date = new Date();
            //date.getDate()获得的是 1970-01-01 08:00:00.011
            //date.getTime()获得的是从 1970-01-01 08:00:00.011开始到现在的毫秒数
            
            //timeStamp可以把得到的毫秒数转换成当前系统时间
            Timestamp timestamp =null;// new Timestamp(date.getTime());
            //输出2015-12-11 08:41:22.391,包括毫秒数
            System.out.println("毫秒数:"+date.getTime());
            //去除时间后面的毫秒数
            //首先设置时间格式
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //System.currentTimeMillis()和date.getTime()一样都是获取的毫秒数
            timestamp=new Timestamp(System.currentTimeMillis());
            //把格式化的时间转换成String类型
            String nowTime=format.format(timestamp);
            System.out.println("转换后的时间:"+nowTime);

            
            
            //02:string转换成日期
            
            
            String time=format.format(date);
            System.out.println(time);
            timestamp=timestamp.valueOf(time);
            System.out.println(timestamp);
        }

原文地址:https://www.cnblogs.com/anshuo/p/5380834.html