开发者

Time - hour minute manipulation

Is there an API to quickly manipulate (e.g. add, subtract) on time (hour, minute).

Pseudo code is listed below

Time t1 = "5 PM";
t1.add(开发者_如何学JAVA"5 minutes");
t1.subtract("90 minutes");


'course there is: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#add%28int,%20int%29

You'll have to set the field parameter appropriately with one of the constants defined in the Field Summary section of the above page


java.time

The standard date-time library of Java SE 8 is rich with all such features.

Note: Quoted below is a notice at the Home Page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Given below is a demo of such features using java.time, the modern API:

import java.time.Duration;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(17, 30);
        System.out.println(time);

        time = time.plusMinutes(5);
        System.out.println(time);

        time = time.minusMinutes(90);
        System.out.println(time);

        // Parsing and formatting
        DateTimeFormatter dtfInput = new DateTimeFormatterBuilder()
                                        .appendPattern("h[:m[:s]] a")   // Optional fields in square bracket
                                        .parseCaseInsensitive()         // Case-insensitive (AM/am/Am etc.)
                                        .toFormatter(Locale.ENGLISH);       
        
        time = LocalTime.parse("5 PM", dtfInput);
        System.out.println(time);
        
        // Dealing with timezone?
        ZonedDateTime zdt = ZonedDateTime.now().with(time);
        System.out.println(zdt);
        
        // Custom format
        DateTimeFormatter timeAndZone12HourFormat = DateTimeFormatter.ofPattern("hh:mm:ss a'['VV']'");
        DateTimeFormatter timeAndZone24HourFormat = DateTimeFormatter.ofPattern("HH:mm:ss'['VV']'");
        System.out.println(zdt.format(timeAndZone12HourFormat)); 
        System.out.println(zdt.format(timeAndZone24HourFormat));
        
        // Adding/subtracting ISO 8601 Duration
        Duration duration = Duration.parse("PT2H30M"); // 2 hours 30 minutes
        zdt = zdt.plus(duration);
        System.out.println(zdt.format(timeAndZone12HourFormat)); 
    }
}

Output:

Learn more about java.time, the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜