如何在java.sql.Time对象中添加30分钟?

时间:2016-04-25 00:35:51

标签: java time

我有这个Time对象:

Time myTime = java.sql.Time.valueOf("15:33:00");

如何在Java中添加30分钟到myTime?这意味着新时间将是16:03:00

2 个答案:

答案 0 :(得分:4)

 java.sql.Time myTime = java.sql.Time.valueOf("15:33:00");
 LocalTime localtime = myTime.toLocalTime();
 localtime = localtime.plusMinutes(30);
 String output = localtime.toString();

你可以直接从java.sql.time获取localTime,你可以在LocalTime Api中使用plusMinutes来增加30分钟。这可能有助于你检查这个

答案 1 :(得分:0)

使用java.time.LocalDateTime

,这是一种简单易行的方法
package time;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalField;

/**
 * Created by Michael
 * Creation date 4/24/2016.
 * @link
 */
public class TimeDemo {

    public static void main(String[] args) {
        // Three three lines do the work
        LocalDateTime localDateTime = LocalDateTime.of(2016, 4, 24, 9, 10);
        LocalDateTime halfHourLater = localDateTime.plusMinutes(30); // Add 30 minutes
        java.sql.Time sqlDateTime = new java.sql.Time(halfHourLater.toInstant(ZoneOffset.UTC).toEpochMilli()); //
        // Printout just to check
        System.out.println(DateTimeFormatter.ofPattern("yyyy-MMM-dd hh:mm:ss.SSS").format(halfHourLater));
        System.out.println("java.time milliseconds: " + halfHourLater.toInstant(ZoneOffset.UTC).toEpochMilli());
        System.out.println("System.currentMillis  : " + System.currentTimeMillis());
    }
}