相当于C#的DateTime.Now在Java? [英] Equivalent of C#'s DateTime.Now in Java?

查看:99
本文介绍了相当于C#的DateTime.Now在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中获取当前日期?

How do I get the current date in Java?

在C#中,它是 DateTime.Now 。 / p>

In C# it is DateTime.Now.

推荐答案

只需构造一个新的 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 for 零参数构造函数


分配一个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. :)

这篇关于相当于C#的DateTime.Now在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆