当前位置:首页 > 编程技术 > 正文内容

Java获取系统当前时间年月日

yc8887个月前 (10-02)编程技术173

Java获取系统当前时间年月日

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


本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!


从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!


本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。


本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/biancheng/10768.html

标签: Java
分享给朋友:

“Java获取系统当前时间年月日” 的相关文章

【说站】Thymeleaf报错Error resolving template “XXX”

【说站】Thymeleaf报错Error resolving template “XXX”

修改了一下开源项目的目录结构访问突然报错Error resolving template “XXX”可能原因有如下三种:第一种可能:原因:在使用springboot的过程中,如果使用thymeleaf...

【说站】用一句话就可以去除宝塔面板操作上的二次验证

【说站】用一句话就可以去除宝塔面板操作上的二次验证

用过宝塔的朋友应该都会发现,现在宝塔面板有些鸡肋的功能,删除文件、删除数据库、删除站点等操作都需要做计算题!不仅加了几秒的延时等待,还无法跳过!这时候就会有朋友在想,如何去除宝塔面板的二次验证,此篇文...

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

这篇文章主要介绍了详解Centos8 配置静态IP的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来学习一下!1. 查看自己的网关地址点击虚...

【说站】利用Webhook实现Java项目自动化部署

【说站】利用Webhook实现Java项目自动化部署

用webhook就能实现Java项目自动部署,其实原理很简单。费话不多说,直接往下看教程。1. 创建gitee仓库并初始化2. 在linux安装git3. 在宝塔的软件的商店里下载Webhook4....

【说站】vagrant实现linux虚拟机的安装并配置网络

【说站】vagrant实现linux虚拟机的安装并配置网络

一、VirtualBox的下载和安装1、下载VirtualBox官网下载:https://www.virtualbox.org/wiki/Downloads我的电脑是Windows的,所以下载Wind...

【说站】linux中redis如何以redis用户重启?

【说站】linux中redis如何以redis用户重启?

通过上图我们可以看到,目前状态是已经以 redis 用户启动着,我想修改下 redis 的密码,然后怎么以 redis 用户重启呢?redis 是 nologin 用户,不能通过 su redis 切...