Java中获取当前时间并格式化
主要有两种方式,其中使用Date比较好控制,代码如下:
//使用Calendar Calendar now = Calendar.getInstance(); System.out.println("年:" + now.get(Calendar.YEAR)); System.out.println("月:" + (now.get(Calendar.MONTH) + 1)); System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH)); System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY)); System.out.println("分:" + now.get(Calendar.MINUTE)); System.out.println("秒:" + now.get(Calendar.SECOND)); //使用Date Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间:" + sdf.format(d));
输出的结果如下:
分类:
编程语言-[Java]
标签:
java
【推荐】AI 的力量,开发者的翅膀:欢迎使用 AI 原生开发工具 TRAE
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 2025年:是时候重新认识System.Text.Json了
· 源码浅析:SpringBoot main方法结束为什么程序不停止
· C#性能优化:为何 x * Math.Sqrt(x) 远胜 Math.Pow(x, 1.5)
· 本可避免的P1事故:Nginx变更导致网关请求均响应400
· 还在手写JSON调教大模型?.NET 9有新玩法
· 2025年:是时候重新认识System.Text.Json了
· .NET 9 的免费午餐:GZip 性能提升38.3%
· 开源新旗舰 GLM-4.5:不想刷榜,只想干活儿
· 优雅的.net REST API之FastEndpoints
· .NET 10 中的新增功能系列文章1——运行时中的新增功能