sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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);
 
    }

 

https://blog.csdn.net/QGhurt/article/details/116425296
posted on   sunny123456  阅读(8997)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示