javadatetimejodatimedate-formatting

How to get integer value of datetime in java?


I'm working in a Java project and I need to get a numeric "value" of a DateTime in Java. For example: the datetime is 2020-07-22T17:40:56.235+05:30 and I want to convert it into 20200722174056235. I am using DateTime methods like getDate(), getYear() to make this kind of value.

Is there any way or any method to covert a datetime into such a numeric value?

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);

I need to use joda DateTime only.


Solution

  • Use a formatter.

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

    Output when I ran the code in my time zone just now:

    20200722210458862

    Alternate way: Only if this is for a library method that I expect to be called often and where efficiency may be a concern, I might consider not formatting and parsing a string. The following gives the same result.

        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();
    

    Did your code work?

    Your code formatted one-digit values into just one character in the string, so your string would typically be too short and miss some zeroes. For example:

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

    java.time

    As @Arvind Kumar Avinash mentioned in a comment, Joda-Time is in maintenance mode and has been succeeded by java.time, the modern Java date and time API: So for anyone having followed Joda-Time’s recommendation to migrate and for all new code, the code using java.time is very similar to the above:

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuuMMddHHmmssSSS");
    
        ZonedDateTime calendar = ZonedDateTime.now(ZoneId.systemDefault());
        String formatted = calendar.format(formatter);
        long numericValue = Long.parseLong(formatted);
    
        System.out.println(numericValue);
    

    Link: Oracle Tutorial: Date Time showing how to use java.time