将日期设置为一行 [英] Set Date in a single line

查看:158
本文介绍了将日期设置为一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Java API,构造函数日期(年,月,日)被折旧。我知道我可以用以下代码替换它:

According to the Java API, the constructor Date(year, month, day) is depreciated. I know that I can replace it with the following code:

Calendar myCal = Calendar.getInstance();
myCal.set(Calendar.YEAR, theYear);
myCal.set(Calendar.MONTH, theMonth);
myCal.set(Calendar.DAY_OF_MONTH, theDay);
Date theDate = myCal.getTime();

但是,我想用更短的东西替换它(理想情况下是一到两行)。

However, I would like something shorter to replace it with (ideally one to two lines).

推荐答案

你可以使用 新的GregorianCalendar(年,月,日) .getTime()


public GregorianCalendar(int year,int month,int dayOfMonth)

构造一个GregorianCalendar,并在默认时区中使用默认语言环境设置给定日期。

Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

这篇关于将日期设置为一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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