我使用此代码将当前时间转换为整数格式
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/