简体   繁体   中英

How to round of a millisecond in java?

Java Code

public static long round(long millis , TimeUnit unit){
    Calendar calendar = Calendar.getInstance();
    switch(unit){
        case DAYS:
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR, 0);
            return calendar.getTimeInMillis();
        case HOURS:
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            return calendar.getTimeInMillis();
        case MINUTES:
            calendar.set(Calendar.MILLISECOND , 0);
            calendar.set(Calendar.SECOND, 0);
            return calendar.getTimeInMillis();
        case SECONDS:
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar.getTimeInMillis();
        case MILLISECONDS:
        default:
            calendar.set(Calendar.MILLISECOND, 0);
            return  calendar.getTimeInMillis();
    }
}

Currently , I am using this code to round of the milliseconds value. In this method redundancy of the code is the big problem.

Is there any better solution or math equations for computing this?

You can eliminate all duplicate lines by taking advantage of fall through like this:

public static long round(long millis , TimeUnit unit){
    Calendar calendar = Calendar.getInstance();
    switch(unit){
        case DAYS:
            calendar.set(Calendar.HOUR, 0);
        case HOURS:
            calendar.set(Calendar.MINUTE, 0);
        case MINUTES:
            calendar.set(Calendar.SECOND, 0);
        case SECONDS:
        case MILLISECONDS:
        default:
            calendar.set(Calendar.MILLISECOND, 0);
    }
    return calendar.getTimeInMillis();
}
public static long round(long millis , TimeUnit unit){
    Calendar calendar = Calendar.getInstance();
    switch(unit){
        case DAYS:
            calendar.set(Calendar.HOUR, 0);
        case HOURS:
            calendar.set(Calendar.MINUTE, 0);
        case MINUTES:
            calendar.set(Calendar.SECOND, 0);
        default:
            calendar.set(Calendar.MILLISECOND, 0);
    }
    return calendar.getTimeInMillis();
}

Note the absence of break or return statements inside the switch . This causes the statements in the switch blocks to fall through to the next case label.

This is covered in the tutorial .

Isn't just calender.set(Calendar.MILLISECOND, 0) sufficient?

Another way to do this would be:

return date.getTime() / 1000 * 1000;

You can take advantage of 'fallthrough' in the switch statement:

public static long round(long millis , TimeUnit unit){
    Calendar calendar = Calendar.getInstance();
    switch(unit){
        case DAYS:
            calendar.set(Calendar.HOUR, 0);
        case HOURS:
            calendar.set(Calendar.MINUTE, 0);
        case MINUTES:
            calendar.set(Calendar.SECOND, 0);
        case SECONDS:
            calendar.set(Calendar.MILLISECOND, 0);
    }
    return  calendar.getTimeInMillis();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM