0

[purpose]

How to get int value after dividing 2 values(long-type).

[problem]

I changed the time(todaySeatedEndDateStr's HH:mm:ss part), but it is impossible to obtain an accurate value.

And I'm not sure that value is correct.

The main formula>

c'' = b / (a+b) * c

  • a, b : long type
  • c : int type
  • c'' : int type

Finally I want to get C''

Situation pic

# Test Code

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeCalculateTest {
    public static void main(String[] args) throws Exception {

        //2016-09-20 00:00:00 (Today's start point)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String todayStartDateStr = "2016-09-20 00:00:00"; 
        Date todayStartDate = sdf.parse(todayStartDateStr);

        //2016-09-19 23:30:00 (Yesterday's particular point)
        String yesterdaySeatedStartDateStr = "2016-09-19 23:30:00";
        Date yesterdaySeatedStartDate = sdf.parse(yesterdaySeatedStartDateStr);

        //2016-09-20 03:30:00 (Today's particular point)
        String todaySeatedEndDateStr = "2016-09-20 21:30:00";
        Date todaySeatedEndDate = sdf.parse(todaySeatedEndDateStr);

        System.out.println("Today's Start Date String : " + todayStartDateStr);
        System.out.println("Today's Start Date Long: " + todayStartDate.getTime());

        System.out.println("Yesterday's Start Date String : " + yesterdaySeatedStartDateStr);
        System.out.println("Yesterday Start Date Long : " + yesterdaySeatedStartDate.getTime());

        System.out.println("Today's End Date String : " + todaySeatedEndDateStr);
        System.out.println("Today's End Date Long : " + todaySeatedEndDate.getTime());

        int c = 500; // <------ c 
        System.out.println("c: " + c);
        if (yesterdaySeatedStartDate.compareTo(todayStartDate) < 0) {

            long a = yesterdaySeatedStartDate.getTime(); // <----- a
            long b = todaySeatedEndDate.getTime(); // <------ b
            long abSum = a + b; // <------ a+b

            System.out.println("Yesterday's long value : " + a);
            System.out.println("Today's long value : " + b);
            System.out.println("---> Sum : " + abSum);

            long result = (long) ((float)b / (float)abSum * c);
            System.out.println("---> Result : " + result);
            System.out.println("------->to int : " + (int)result );
        }
    }
}

output >

Today's Start Date String : 2016-09-20 00:00:00
Today's Start Date Long: 1474297200000
Yesterday's Start Date String : 2016-09-19 23:30:00
Yesterday Start Date Long : 1474295400000
Today's End Date String : 2016-09-20 21:30:00
Today's End Date Long : 1474374600000
c: 500
Yesterday's long value : 1474295400000
Today's long value : 1474374600000
---> Sum : 2948670000000
---> Result : 250
------->to int : 250

I changed the 'todaySeatedEndDateStr' variable's HH:mm:ss,

but always get the 250.

How can I fix this problem?

plz help me..

J-Hyun
  • 13
  • 2
  • 1
    This seems like an XY problem, what are you trying to do here? I'm sure there is a better way to do it using `java.time`. – Jorn Vernee Sep 20 '16 at 09:52
  • 1
    What value for `result` are you actually expecting? `250` looks about right to me. – Jorn Vernee Sep 20 '16 at 09:58
  • I changed time 2016-09-20 01:00:00 -> 2016-09-20 23:00:00. but, result always 250. I expected another value. If I change the time, I expected the ratio would be different. – J-Hyun Sep 20 '16 at 10:07
  • oh, I developed on Java 6 version. – J-Hyun Sep 20 '16 at 10:09
  • Yes, but most of that number falls behind the decimal point, which is cut off when you convert to an integer (`long`/`int`/others) so you don't see the change. Try and make `result` a `double` instead of `long`. – Jorn Vernee Sep 20 '16 at 10:10
  • this case, I get the needed value success. >> int vv = 950; int v1 = 2; int v2 = 8; int sum = v1+v2; int ratio = (int) (Math.round((float)v2 / sum * 100d)); System.out.println("result : " + (float)(vv / sum) * ratio * 0.1); – J-Hyun Sep 20 '16 at 10:36
  • When I calculate it the same logic, I could not get the desired value. – J-Hyun Sep 20 '16 at 11:32
  • You don't have the same logic, the output of that is a `double`, that's being printed directly, but you're casting to a `long` first, and then printing. – Jorn Vernee Sep 20 '16 at 11:34
  • @Jorn Vernee I solv this problem from answer, I should study more about java language and the type casting.... Thank you! – J-Hyun Sep 20 '16 at 11:56

2 Answers2

0

a and b are both measured in milliseconds since January 1970, so they are roughly the same even though a is yesterday and b is today. So b / (a + b) is very close to a half, and half of 500 is 250.

If I interpret your graph correctly, you want to do this instead:

        long midnight = todayStartDate.getTime();
        long result = (long) ((float) (b - midnight) / (float) (b - a) * c);

Now I am taking the time since midnight in proportion to the time since a yesterday. Please try and see if it works for you.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Midnight of earlier period’s start to end of later period?

Looks like you have over-complicated the original problem. Seems your problem picture wants elapsed time from midnight of the date of the earlier period to a later moment. If not so, please edit your Question to state in plain conversational English what is the problem statement. And why do you use words "yesterday" and "today" if the date-time values are hard-coded?

java.time

You are using troublesome old legacy date-time classes now supplanted by the java.time classes.

We parse as LocalDateTime objects because your inputs lack info about offset-from-UTC or time zone. If you want to account for issues such as Daylight Saving Time (DST), use ZonedDateTime instead.

To parse, we replace the SPACE in the middle with a T to comply with ISO 8601 standard.

LocalDateTime earlierStart = LocalDate.parse( "2016-09-19 23:30:00".replace( " " , "T" ) );

LocalDateTime laterStop = LocalDate.parse( "2016-09-20 21:30:00".replace( " " , "T" ) );

To get the midnight ending of the starting point, we need to go through the LocalDate. We move to the start of the next day because getting the last moment of the day is problematic with an endlessly divisible fractional second.

LocalDate localDateOfStartNextDay = earlierStart.toLocalDate().plusDays( 1 );
LocalDateTime newDayAfterStart = localDateOfStartNextDay.atStartOfDay();

Now capture the elapsed time as a Duration with a resolution of nanoseconds.

Duration duration = Duration.between( newDayAfterStart , laterStop );

This code may not be exactly your solution, given that your Question is confusing. But I think you can see that working with the java.time classes will be less convoluted that trying to do math on count-from-epoch numbers.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154