Obito Uchiha
Obito Uchiha

Reputation: 113

Problems in converting a given date to minutes

I have a function where I fetch date and convert it into milliseconds and then I change it to minutes. The final value is always positive but for some unknown reasons, the dates from Jun 5 to Jun 29 the resultant value is coming as negative. Because of this my subsequent operations which expect positive values get affected. What's the root cause of this problem ?. I have attached my piece of code with different snapshots of results for different dates. And can anyone suggest a workaround for this issue? I am using java version "1.8.0_161"

String democurrentDate = "Wed Jul 4 17:28:41 IST 2020";                    
Date entrydatecom = new Date();
entrydatecom = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(democurrentDate);
System.out.println("The current date in correct format for milli ==>" + entrydatecom);                    
int milli1 = (int) entrydatecom.getTime();
System.out.println("Milli === > " + milli1);
long minutesforcurr = TimeUnit.MILLISECONDS.toMinutes(milli1);
System.out.println("The minutes for current date is ==> " + minutesforcurr);

When the date is July 4

The current date in correct format for milli ==>Sat Jul 04 17:28:41 IST 2020
Milli === > 431054184
The minutes for current date is ==> 7184

When the date is Jun 4

The current date in correct format for milli ==>Thu Jun 04 17:28:41 IST 2020
Milli === > 2134021480
The minutes for current date is ==> 35567

When the date is Jun 17

The current date in correct format for milli ==>Wed Jun 17 17:28:41 IST 2020
Milli === > -1037745816
The minutes for current date is ==> -17295

When the date is Jun 22

The current date in correct format for milli ==>Mon Jun 22 17:28:41 IST 2020
Milli === > -605745816
The minutes for current date is ==> -10095

Upvotes: 1

Views: 231

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79015

Problem

A major problem in your date/time string is that 4th Jul 2020 is Sat, whereas you have written Wed and therefore either it won't be parsed correctly or will cause exception to be thrown.

Apart from that, I recommend you use modern date/time API instead of using broken java.util.Date and SimpleDateFormat.

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

class Main {
    public static void main(String[] args) {
        // Define format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss zzz yyyy");

        // Parse to ZonedDateTime
        ZonedDateTime zdt = LocalDateTime.parse("Sat Jul 4 17:28:41 IST 2020", formatter)
                .atZone(ZoneId.of("Asia/Kolkata"));

        // Calculate duration in minute
        long minutes = ZonedDateTime.now(ZoneId.of("Asia/Kolkata")).until(zdt, ChronoUnit.MINUTES);

        // Display
        System.out.println(minutes);
    }
}

Output:

24333

Upvotes: 3

Mhamad El Itawi
Mhamad El Itawi

Reputation: 254

use: Long milli1 = entrydatecom.getTime(); When you are casting a long to an int you are loosing a lot of data. an int number can support number between: -2147483648 and 2147483647 a long number is between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807

By example if you are testing for "Wed Jun 17 17:28:41 IST 2020" the number of ms will be: 1592407721000 ( > 2147483647 ) it can't fit

Upvotes: 2

chburd
chburd

Reputation: 4159

You should use long, don't cast to int.

Upvotes: 3

Related Questions