Java获取系统当前时间年月日
2024-07-31
27
在Java中使用java.util.Date类和java.time.LocalDate类来获取系统当前的时间,年份,月份和日期。
一、使用java.util.Date类获得当前日期
java.util.Date类表示特定的瞬间,精确到毫秒。下面是获取当前日期和时间的代码示例:
import java.util.Date; public class GetCurrentDateTime { public static void main(String[] args) { // create a date object with the current date and time. Date date = new Date(); // print the date System.out.println("当前日期和时间: " + date.toString()); } }
二、使用java.time.LocalDate获取当前年月日
使用Java 8引入的java.time包中的LocalDate类,可以很方便地获取当前日期的年份,月份和日子。以下是操作的具体代码:
import java.time.LocalDate; public class GetCurrentDate { public static void main(String[] args) { // get the current date LocalDate currentDate = LocalDate.now(); // get the year int year = currentDate.getYear(); // get the month int month = currentDate.getMonthValue(); // get the day int day = currentDate.getDayOfMonth(); // print the current date System.out.println("当前年份: " + year); System.out.println("当前月份: " + month); System.out.println("当前日期: " + day); } }
三、使用java.text.SimpleDateFormat格式化日期
java.text.SimpleDateFormat是一个可以以用户定义的模式格式化 Date 对象的具体类。
import java.text.SimpleDateFormat; import java.util.Date; public class FormatCurrentDateTime { public static void main(String[] args) { // create a date object with the current date and time. Date now = new Date(); // create an instance of SimpleDateFormat SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // format the date String currentDateAndTime = formatter.format(now); // print the date and time System.out.println("当前年月日和时间: " + currentDateAndTime); } }更新于:1个月前
赞一波!
相关文章
- TP6.0 空控制器: 健壮系统服务
- TP6 时间查询
- .NET中运行Java代码
- 使用IKVM.NET在.NET中运行Java代码
- 系统的讲解网站的优化
- TP6.0 系统服务的使用和理解
- Windows 系统 PhpStorm 2020无限试用30天
- nesbot/carbon 日期时间处理扩展包
- Linux中date命令如何格式化输出需要的时间格式
- 修改人人商城核销码有效时间
- Navicat 闲置时间过长会卡死
- C#中如何获取当前系统CPU的核心数
- java读入一行输入
- C#获取Windows系统中所有已安装软件的名称和版本号
- 在64位的Windows系统下运行32位的C#程序如何控制重定向
- 怎样在Linux系统 Ubuntu18.04 中安装微信
- cms系统套标签的简单介绍
- java冒泡排序
- 有没有一个在线工具可以将Python代码转换为Java代码?
- .net core md5加密与java不一致
文章评论
全部评论