java操作时间方式基础教程demo

 更新时间:2023年10月18日 11:18:05   作者:bug生产者  
这篇文章主要为大家介绍了java操作时间方式demo基础教程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

java操作时间的方式

获取年月日时分秒

public class Test {
    public static void main(String[] args) {
        System.out.println("----------使用Calendar--------------------");
        Calendar cal = Calendar.getInstance();
        System.out.println("年"+cal.get(Calendar.YEAR));
        System.out.println("月"+(cal.get(Calendar.MONTH)+1)); // Calendar.MONTH  获取到的是0-11
        System.out.println("日"+cal.get(Calendar.DATE));
        System.out.println(cal.get(Calendar.HOUR)); // 12小时制的小时
        System.out.println("时"+cal.get(Calendar.HOUR_OF_DAY)); // 24小时制的小时
        System.out.println("分"+cal.get(Calendar.MINUTE));
        System.out.println("秒"+cal.get(Calendar.SECOND));
        System.out.println("--------------使用java8的LocalDateTime----------------");
        LocalDateTime local = LocalDateTime.now();
        System.out.println("年"+local.getYear());
        System.out.println(local.getMonth().name());  // 英文的月
        System.out.println("月"+local.getMonthValue());  // 阿拉伯数字 相当于local.getMonth().getValue()
        System.out.println("日"+local.getDayOfMonth());
        System.out.println("时"+local.getHour()); // 24小时制的小时
        System.out.println("分"+local.getMinute());
        System.out.println("秒"+local.getSecond());
    }
}

时间格式化中的字符含义

字符描述
G时代指示器(AD)
y年(2001)
M月(07)
d天(20)
h带有A.M./P.M.的小时(1~12)
H小时(0~23)
m分钟(0~59)
s秒(0~59)
S毫秒
E周几(星期四)
D一年中的第几天
w一年中的第几周
W一月中的第几周
aA.M./P.M.标记
k一天中的第几个小时(1~24)
K带有A.M./P.M.的小时
z时区
DateFormat format = new SimpleDateFormat("yyyy.MM.dd E"); //2021.01.14 星期四
System.out.println(format.format(new Date()));
// 一年中的第几天
format = new SimpleDateFormat("yyyy.MM.dd D"); //2021.01.14 14
System.out.println(format.format(new Date()));
// 一年中的第几周
format = new SimpleDateFormat("yyyy.MM.dd w"); //2021.01.14 3
System.out.println(format.format(new Date()));
// 一月中的第几周
format = new SimpleDateFormat("yyyy.MM.dd W"); //2021.01.14 3
System.out.println(format.format(new Date()));
// A.M./P.M.标记
format = new SimpleDateFormat("yyyy.MM.dd a"); //2021.01.14 下午
System.out.println(format.format(new Date()));
// 一天中的第几个小时(1~24)
format = new SimpleDateFormat("yyyy.MM.dd k"); //2021.01.14 14
System.out.println(format.format(new Date()));
// 带有A.M./P.M.的小时
format = new SimpleDateFormat("yyyy.MM.dd K"); //2021.01.14 2
System.out.println(format.format(new Date()));
// 时区
format = new SimpleDateFormat("yyyy.MM.dd z"); //2021.01.14 14
System.out.println(format.format(new Date()));

以上就是java操作时间方式的详细内容,更多关于java操作时间方式的资料请关注脚本之家其它相关文章!

相关文章

  • 剑指Offer之Java算法习题精讲字符串操作与数组及二叉搜索树

    剑指Offer之Java算法习题精讲字符串操作与数组及二叉搜索树

    跟着思路走,之后从简单题入手,反复去看,做过之后可能会忘记,之后再做一次,记不住就反复做,反复寻求思路和规律,慢慢积累就会发现质的变化
    2022-03-03
  • Spring Boot详解配置文件有哪些作用与细则

    Spring Boot详解配置文件有哪些作用与细则

    SpringBoot项目是一个标准的Maven项目,它的配置文件需要放在src/main/resources/下,其文件名必须为application,其存在两种文件形式,分别是properties和yaml(或者yml)文件
    2022-07-07
  • 使用Feign传递请求头信息(Finchley版本)

    使用Feign传递请求头信息(Finchley版本)

    这篇文章主要介绍了使用Feign传递请求头信息(Finchley版本),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • java拼接字符串时去掉最后一个多余逗号的方法

    java拼接字符串时去掉最后一个多余逗号的方法

    这篇文章主要介绍了java拼接字符串时去掉最后一个多余逗号的方法,实例分析了java操作字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • SpringMVC异常处理的三种方式

    SpringMVC异常处理的三种方式

    在SpringMVC中异常处理是一个重要的方面,它帮助我们有效地处理应用程序中的异常情况,提高用户体验和系统的稳定性,这篇文章主要给大家介绍了关于SpringMVC异常处理的三种方式,需要的朋友可以参考下
    2024-02-02
  • Mybatisplus主键生成策略算法解析

    Mybatisplus主键生成策略算法解析

    这篇文章主要介绍了Mybatisplus主键生成策略算法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • springboot加载命令行参数ApplicationArguments的实现

    springboot加载命令行参数ApplicationArguments的实现

    本文主要介绍了springboot加载命令行参数ApplicationArguments的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • 深入解析java HashMap实现原理

    深入解析java HashMap实现原理

    这篇文章主要介绍了深入解析java HashMap实现原理的相关资料,需要的朋友可以参考下
    2015-09-09
  • SpringBoot中手动开启事务的实现方法

    SpringBoot中手动开启事务的实现方法

    在Spring Boot中,除了使用@Transactional注解外,还可以通过TransactionTemplate或PlatformTransactionManager手动控制事务,本文给大家介绍SpringBoot中如何手动开启事务,感兴趣的朋友跟随小编一起看看吧
    2025-11-11
  • logback的DuplicateMessageFilter日志过滤操作源码解读

    logback的DuplicateMessageFilter日志过滤操作源码解读

    这篇文章主要为大家介绍了logback的DuplicateMessageFilter日志过滤操作源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11

最新评论