Java:如何只增加日期中的时间? [英] Java: How to increment only the time in a date?

查看:63
本文介绍了Java:如何只增加日期中的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要将日期中的时间增加1小时。



I need to increment only the time in a date by 1 hour.

Calendar cal=Calendar.getInstance();
cal.setTime(Calendar.HOUR,1);
cal.getTime();





这是我试图增加时间的代码,但这也增加了它的日期。

有人可以帮我解决这个问题吗?



This is the code I tried to increment time,but this also increments date with it.
Can anybody help me to solve this?

推荐答案

试试这个:

Try this:
package com.peterleow;

import java.util.Calendar;

public class exercise1 {

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        System.out.println(calendar.getTime());

        calendar.add(Calendar.HOUR,  1);

        System.out.println(calendar.getTime());

    }

}



了解更多:类日历 [ ^ ]


这篇关于Java:如何只增加日期中的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆