Java获取当前时间并转化为yyyy-MM-dd HH:mm:ss格式的多种方式

 更新时间:2024年03月25日 10:10:15   作者:一蓑烟雨任平生2024  
这篇文章主要介绍了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);
    }

到此这篇关于Java获取当前时间并转化为yyyy-MM-dd HH:mm:ss格式的文章就介绍到这了,更多相关java获取当前时间内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java.lang.NoClassDefFoundError错误解决办法

    java.lang.NoClassDefFoundError错误解决办法

    这篇文章主要介绍了java.lang.NoClassDefFoundError错误解决办法的相关资料,需要的朋友可以参考下
    2017-06-06
  • Mybatis 自定义类型处理器示例详解

    Mybatis 自定义类型处理器示例详解

    在某些情况下我们需要对类型做处理,例如数据存储的是Long,程序里是BigDecimal,那么我们出库入库都需要做处理,此时就可以使用类型处理器,本文通过示例给大家介绍Mybatis 自定义类型处理器的相关知识,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • SpringBoot详解如果通过@Value注解给静态变量注入值

    SpringBoot详解如果通过@Value注解给静态变量注入值

    这篇文章主要介绍了springboot如何通过@Value给静态变量注入值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 深入理解equals和hashCode方法

    深入理解equals和hashCode方法

    在Java中,equals和hashCode方法是Object中提供的两个方法,这两个方法对以后的学习有很大的帮助,本文就深度来去讲解这两个方法。下面小编带大家来一起学习吧
    2019-06-06
  • SpringCloud中Zuul网关原理及其配置

    SpringCloud中Zuul网关原理及其配置

    Spring Cloud是一个基于Spring Boot实现的微服务应用开发工具,其中的Zuul网关可以实现负载均衡、路由转发、鉴权、限流等功能,本文将从Spring Cloud中Zuul网关的原理、使用场景和配置过程详细介绍,帮助大家更好地了解和应用Zuul网关,需要的朋友可以参考下
    2023-06-06
  • Java ObjectMapper使用详解

    Java ObjectMapper使用详解

    ObjectMapper类是Jackson的主要类,它可以帮助我们快速的进行各个类型和Json类型的相互转换,本文给大家介绍Java ObjectMapper的相关知识,感兴趣的朋友跟随小编一起看看吧
    2024-07-07
  • Spring Boot 使用 WebServiceTemplate 调用 WebService的完整步骤

    Spring Boot 使用 WebServiceTemplate 调用&nbs

    文章介绍了使用Spring WebServiceTemplate调用WebService的方法,包括添加依赖、配置WebServiceTemplate、编写调用工具类、注意事项及完整的调用步骤,特别强调了调用.NET的WebService需要添加SOAPAction请求头,感兴趣的朋友跟随小编一起看看吧
    2026-03-03
  • JDK17安装教程以及其环境变量配置教程

    JDK17安装教程以及其环境变量配置教程

    环境变量对Java初学者来说真的是一件头疼的事,本人也经历过这样的事情,这篇文章主要给大家介绍了关于JDK17安装教程以及其环境变量配置的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-03-03
  • Java实现弹窗效果的基本操作(2)

    Java实现弹窗效果的基本操作(2)

    这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • 使用Java实现为PPT(PowerPoint)设置背景

    使用Java实现为PPT(PowerPoint)设置背景

    在日益数字化的办公环境中,PowerPoint 演示文稿已成为信息传达不可或缺的工具,本文将深入探讨如何利用 Spire.Presentation for Java 为PowerPoint幻灯片设置纯色、渐变色和图片背景,感兴趣的可以了解下
    2025-08-08

最新评论