java - 如何单独检索时间字段?

标签 java datetime

我使用此代码将当前时间转换为整数格式

 long x1=new Date().getTime();
 int x=(int) x1;

现在我想从整数 int x 中分别获取实际日期值(年,月,日期,小时,分钟) .

我怎样才能做到这一点?

谢谢(请指导我)

最佳答案

使用 java 8,您可以在 LocalDate 中轻松管理它

    LocalDate today = LocalDate.now();
    int thisYear = today.getYear();
    int thisMonth = today.getMonthValue();
    int thisDay = today.getDayOfMonth();
    out.println(thisYear + "-" + thisMonth + "-" + thisDay);

但是当您尝试实现更多(小时、分钟、秒)时,您可以转向 LocalDateTime

    LocalDateTime curMoment = LocalDateTime.now();
    thisYear = curMoment.getYear();
    thisMonth = curMoment.getMonthValue();
    thisDay = curMoment.getDayOfMonth();
    int thisHour = curMoment.getHour();
    int thisMinute = curMoment.getMinute();
    int thisSecond = curMoment.getSecond();
    System.out.println(thisYear + "-" + thisMonth + "-" + thisDay + " " + thisHour + ":" + thisMinute + ":" + thisSecond);

然后输出将是:+

2018-7-4
2018-7-4 14:33:12

关于java - 如何单独检索时间字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51167048/

相关文章:

python - pd.to_datetime值错误: Given date string not likely a datetime

java - 使用静态方法更改静态变量

java - 测试文件是否为图像文件

Java - HTTP Post,使用 cookie 登录网站,与此 PHP cURL 代码等效的 java?

java - 在java中获取List <int>的.class

python3.5/pandas - 将多列转换为日期时间

perl - 为什么在没有引号的情况下,Perl 中的 IF 条件下无法解析数字

c++ - 在 C++ 中以 mm/dd/yyyy 获取当前时间

JAVA:如何实现带有join语句的sql的延迟加载?

c# - 可为空的 DateTime 扩展引发 'does not contain a definition' 异常