如何获取当前日期和时间

编程入门 行业动态 更新时间:2024-10-24 18:26:19
本文介绍了如何获取当前日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何获取Java中的当前日期和时间?

How do I get the current date and time in Java?

我正在寻找与C#中的DateTime.Now等效的东西.

I am looking for something that is equivalent to DateTime.Now from C#.

推荐答案

只需构造一个没有任何参数的新Date对象;这会将当前日期和时间分配给新对象.

Just construct a new Date object without any arguments; this will assign the current date and time to the new object.

import java.util.Date; Date d = new Date();

用 Javadocs的话来说用于零参数构造函数:

分配一个Date对象并初始化它,以便它表示分配它的时间,以最近的毫秒为单位.

Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

请确保您使用的是java.util.Date而不是java.sql.Date-后者没有零参数构造函数,并且语义完全不同,它们是完全不同的对话的主题. :)

Make sure you're using java.util.Date and not java.sql.Date -- the latter doesn't have a zero-arg constructor, and has somewhat different semantics that are the topic of an entirely different conversation. :)

更多推荐

如何获取当前日期和时间

本文发布于:2023-10-31 07:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!