0

I am searching for a method to get the last third of two dates.

first example:

Date 1 = 22:00

Date 2 = 01:00 (next day)

calculateLastThird(); (output: 00:00)

second example:

Date 1 = 22:25

Date 2 = 01:45 (next day)

calculateLastThird(); (output: 00:38)

I already know how to get the midpoint between those two dates:

Date midpoint = new Date((date1.getTime() + date2.getTime()) / 2);
3
  • 2
    I recommend you don’t use Date. That class is poorly designed and long outdated. Instead you probably want to use either LocalTime or ZonedDateTime. Both are from java.time, the modern Java date and time API. Commented Dec 26, 2020 at 14:33
  • What you mean by last third of date? Commented Dec 26, 2020 at 14:33
  • 2
    @HarinderSingh I think it’s easier to understand from the examples. I take it as the point when two thirds of the time between the two have elapsed. From 22 to 1 is three hours, Two thirds is 2 hours, and 2 hours after 22 is 00:00. Commented Dec 26, 2020 at 14:35

3 Answers 3

1

java.time

I recommend that you use java.time, the modern Java date and time API, for your date and time work.

    ZoneId zone = ZoneId.of("America/Boise");
    ZonedDateTime zdt1 = ZonedDateTime.of(2020, 12, 26, 22, 0, 0, 0, zone);
    ZonedDateTime zdt2 = ZonedDateTime.of(2020, 12, 27, 1, 0, 0, 0, zone);
    
    Duration fullElapsedTime = Duration.between(zdt1, zdt2);
    Duration twoThirds = fullElapsedTime.multipliedBy(2).dividedBy(3);
    ZonedDateTime lastThird = zdt1.plus(twoThirds);
    
    System.out.println(lastThird);

Output from this snippet is:

2020-12-27T00:00-07:00[America/Boise]

Three things I like about this code are:

  1. It pretty well mimics the way one would do the calculation by hand and how you would explain to someone else which calculation you want at all.
  2. It takes any transistion to or from summer time (DST) into account.
  3. It leaves the actual calculation to the library methods. It involves no low-level addition or division in your own code.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Sign up to request clarification or add additional context in comments.

Comments

0

One way to do this is the following:

Date midpoint = new Date((date1.getTime() + 2 * date2.getTime()) / 3);

Comments

0

If you insist using Date.
It should be a simple math:

  1. First, need to calculate the difference between the two dates (in milliseconds).
  2. Then get the 2/3 rd of that value and add it to the first date.
private static Date calculateLastThird(Date d1, Date d2) { 
        // Calculate time difference in milliseconds 
        long differenceMillis = d2.getTime() - d1.getTime();
        double millisToAdd = (2.0/3.0) * differenceMillis;
        
        Date twoThird = new Date(d1.getTime() + (long) millisToAdd);
        
        return twoThird;
    }
    
    public static void main(String args[]) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            Date date1= sdf1.parse("2020/12/26 22:00:00");
            Date date2= sdf1.parse("2020/12/27 01:00:00");
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
            System.out.println("Last third: " + sdf2.format(calculateLastThird(date1, date2)));
            
            Date date3= sdf1.parse("2020/12/26 22:25:00");
            Date date4= sdf1.parse("2020/12/27 01:45:00");
            System.out.println("Last third: " + sdf2.format(calculateLastThird(date3, date4)));
            
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }

Sample output of above code:

Last third: 00:00
Last third: 00:38

1 Comment

These terrible date-time classes were supplanted years ago by the modern java.time classes defined in JSR 310. Sun, Oracle, and the JCP community have given up on these legacy classes. So should we all. See modern solution in Answer by Ole V.V..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.