java 获取当前时间并转化为yyyy-MM-dd HH:mm:ss格式 时间戳和字符串之间转换
方法一(线程不安全, 不建议使用)
- private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
-  Date now = new Date();
-  String time = sdf.format(now);
方法二(线程安全,建议使用)
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
-  
- public class testMain {
-  
-     public static void main(String[] args) {
-        // yyyy-MM-dd HH:mm:ss.SSS  ---> 年-月-日 时-分-秒-毫秒   (想删掉哪个小部分就直接删掉哪个小部分)
-  
-         String timeStr1=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-         String timeStr2=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
-         System.out.println("当前时间为:"+timeStr1);
-         System.out.println("当前时间为:"+timeStr2);
-     }
- }
-  
运行结果:
当前时间为:2018-11-27 10:41:47
 当前时间为:2018-11-27 10:41:47.392
时间转时间戳:
- /* 
-      * 将时间转换为时间戳
-      */    
-     public static String dateToStamp(String s) throws ParseException{
-         String res;
-         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-         Date date = simpleDateFormat.parse(s);
-         long ts = date.getTime();
-         res = String.valueOf(ts);
-         return res;
-     }
- /* 
-      * 将时间戳转换为时间
-      */
-     public static String stampToDate(String s){
-         String res;
-         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-         long lt = new Long(s);
-         Date date = new Date(lt);
-         res = simpleDateFormat.format(date);
-         return res;
-     }
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-  
- public class Test2 {
- 	public static void main(String[] args) {
- 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- 		System.out.println(sdf.format(new Date()));
- 		//获取当前时间戳,也可以是你自已给的一个随机的或是别人给你的时间戳(一定是long型的数据)
- 		long timeStamp = System.currentTimeMillis();  
- 		//这个是你要转成后的时间的格式
- 		SimpleDateFormat sdff=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- 		// 时间戳转换成时间
- 		String sd = sdff.format(new Date(timeStamp));   
- 		System.out.println(sd);//打印出你要的时间
- 	}
- 	/* 
-      * 将时间转换为时间戳
-      */    
-     public static String dateToStamp(String s) throws ParseException {
-         String res;
-         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-         Date date = simpleDateFormat.parse(s);
-         long ts = date.getTime();
-         res = String.valueOf(ts);
-         return res;
-     }
-  
-     /* 
-      * 将时间戳转换为时间
-      */
-     public static String stampToDate(String s){
-         String res;
-         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-         long lt = new Long(s);
-         Date date = new Date(lt);
-         res = simpleDateFormat.format(date);
-         return res;
-     }
- }
-     @Test
-     public void test1(){
-         /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
-         System.out.println(sdf.format(new Date()));*/
-  
-         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
-         System.out.println(sdf.format(new Date()));
-  
-         long currentTimeMillis = System.currentTimeMillis();
-         System.out.println(currentTimeMillis);
-         SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-         String format = sdf2.format(new Date(currentTimeMillis));
-         System.out.println(format);
-  
-     }
 
                    
                 

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号