【问题标题】:How to get integer value of datetime in java? [duplicate]如何在java中获取日期时间的整数值? [复制]
【发布时间】:2020-11-12 00:03:34
【问题描述】:

我在一个 Java 项目中工作,我需要在 Java 中获取 DateTime 的数字“值”。例如:日期时间为2020-07-22T17:40:56.235+05:30,我想将其转换为20200722174056235。我正在使用DateTimegetDate()getYear() 方法来生成这种值。

有没有什么办法或方法可以将日期时间转换成这样的数值?

DateTime calendar = new DateTime();

        int year       = calendar.getYear();
        int month      = calendar.getMonthOfYear();
        int dayOfMonth = calendar.getDayOfMonth();            
        int hour       = calendar.getHourOfDay();// 12 hour clock
        int minute     = calendar.getMinuteOfHour();
        int second     = calendar.getSecondOfMinute();
        int millisecond= calendar.getMillisOfSecond();
       
        String dt = String.valueOf((year)+
                String.valueOf(month)+
                String.valueOf(dayOfMonth)+
                String.valueOf(hourOfDay)+
                String.valueOf(minute)+
                String.valueOf(second)+
                String.valueOf(millisecond));
        return Long.valueOf(dt);

我只需要使用 joda DateTime

【问题讨论】:

  • calendar 的确切类型是什么,您开始使用的对象?是乔达日期时间吗?
  • 您能否澄清一下问题:DateTime() 是什么?你的意思是java.time.ZonedDateTime 类?但是代码 sn-p 似乎适用于旧版java.util.Calendar 类?
  • 我有更新帖
  • 您需要使用 Joda 的DateTime 还是可以使用其他方式? Java 有一个很棒的新时间 API (java.time),它的灵感来自 Joda。
  • 是的,我只需要使用 joda datetime

标签: java datetime jodatime date-formatting


【解决方案1】:

使用格式化程序。

    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMddHHmmssSSS");
    
    DateTime calendar = new DateTime();
    String formatted = calendar.toString(formatter);
    Long numericValue = Long.parseLong(formatted);
    
    System.out.println(numericValue);

刚才在我的时区运行代码时的输出:

20200722210458862

替代方式:仅当这是我希望经常调用的库方法并且可能需要考虑效率时,我可能会考虑不格式化和解析字符串。下面给出了相同的结果。

    long numericValue = calendar.getYear();
    numericValue = numericValue * 100 + calendar.getMonthOfYear();
    numericValue = numericValue * 100 + calendar.getDayOfMonth();
    numericValue = numericValue * 100 + calendar.getHourOfDay();
    numericValue = numericValue * 100 + calendar.getMinuteOfHour();
    numericValue = numericValue * 100 + calendar.getSecondOfMinute();
    numericValue = numericValue * 1000 + calendar.getMillisOfSecond();

您的代码有效吗?

您的代码可能将一位数字值格式化为字符串中的一个字符,因此您的字符串通常会太短并且会丢失一些零。例如:

Correct:        20200722210458862 (2020 07 22 21 04 58 862)
From your code:   202072221458862 (2020  7 22 21  4 58 862)

【讨论】:

    【解决方案2】:

    这里是代码sn-p

    public class ValidateString {
        public static void main(String[] args) {
            String pattern = "yyyyMMddHHmmss"; // Change the pattern occording to your need
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            String date = simpleDateFormat.format(new Date());
            System.out.println(date);
        }
    }
    

    【讨论】:

    • 请不要教年轻人使用早已过时且臭名昭著的SimpleDateFormat类。至少不是第一选择。而且不是没有任何保留。今天我们在java.time, the modern Java date and time API, 和它的DateTimeFormatter 中做得更好。
    猜你喜欢
    • 2013-06-14
    • 1970-01-01
    • 2012-06-18
    • 2019-08-23
    • 2013-11-29
    • 2011-12-21
    • 2012-12-10
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多