如何在java中获取datetime的整数值?

如何在java中获取datetime的整数值?,java,datetime,jodatime,date-formatting,Java,Datetime,Jodatime,Date Formatting,我在一个Java项目中工作,我需要在Java中获得一个DateTime的数字“值”。例如:日期时间是2020-07-22T17:40:56.235+05:30,我想将其转换为20200722174056235。我正在使用DateTime方法,如getDate(),getYear()来生成这种值 有没有办法或方法将日期时间转换成这样的数值 DateTime calendar = new DateTime(); int year = calendar.getYear()

我在一个Java项目中工作,我需要在Java中获得一个
DateTime
的数字“值”。例如:日期时间是
2020-07-22T17:40:56.235+05:30
,我想将其转换为
20200722174056235
。我正在使用
DateTime
方法,如
getDate()
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

这是代码片段

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);
    }
}
使用格式化程序

    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)

您开始使用的对象是什么类型的日历?是joda DateTime吗?你能澄清这个问题吗:什么是
DateTime()
?你是说
java.time.ZonedDateTime
类吗?但是代码片段似乎适用于遗留的
java.util.Calendar
类?我有更新帖子您需要使用Joda的
DateTime
还是其他什么?Java有一个很棒的新时间API(),它的灵感来自Joda。是的,我只需要使用Joda datetime。请不要教年轻人使用过时且臭名昭著的
SimpleDateFormat
类。至少不是第一个选择。而且不是毫无保留的。今天,我们在及其
DateTimeFormatter
中有了更好的功能。