Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 除了SimpleDateFormat之外,我如何在设置的时间上增加一个小时?_Java_Android_Eclipse_Simpledateformat - Fatal编程技术网

Java 除了SimpleDateFormat之外,我如何在设置的时间上增加一个小时?

Java 除了SimpleDateFormat之外,我如何在设置的时间上增加一个小时?,java,android,eclipse,simpledateformat,Java,Android,Eclipse,Simpledateformat,我有一个显示当前时间的开始按钮,我希望能够有一个停止时间比当前时间晚一小时的按钮。我该怎么做呢 这是显示当前时间的按钮的代码 Button stopButton = (Button) this .findViewById(R.id.StartTrackingEditStopTime_button); // using SimpleDateFormat class SimpleDateFormat sdfStopTime = new SimpleDateFo

我有一个显示当前时间的开始按钮,我希望能够有一个停止时间比当前时间晚一小时的按钮。我该怎么做呢

这是显示当前时间的按钮的代码

Button stopButton = (Button) this
            .findViewById(R.id.StartTrackingEditStopTime_button);
    // using SimpleDateFormat class
    SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a",
            Locale.ENGLISH);
    String newStoptime = sdfStopTime
            .format(new Date(System.currentTimeMillis()));

    stopButton.append(newStoptime);

非常感谢您提供有关如何执行此操作的帮助或建议。

使用
日历
类()。假设您现在已经有了
日期

Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.HOUR, 1);
Date inAnHour = calendar.getTime();

// format inAnHour with your DateFormat and set a button label

使用
日历
类()。假设您现在已经有了
日期

Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
calendar.add(Calendar.HOUR, 1);
Date inAnHour = calendar.getTime();

// format inAnHour with your DateFormat and set a button label

您必须在当前时间的基础上增加一小时

// You don't have to put System.currentTimeMillis() to the constructor.
// Default constructor of Date gives you the current time.
Date stopTime = new Date();
stopTime.setHours(stopTime.getHours() + 1);
String newStoptime = sdfStopTime.format(stopTime);
stopButton.append(newStoptime);
如果您不想使用不推荐使用的函数
setHours
getHours
, 使用
setTime
getTime
并将3600000毫秒(1小时)添加到当前时间:

stopTime.setTime(stopTime.getTime() + 60 * 60 * 1000);
而不是

stopTime.setHours(stopTime.getHours() + 1);

您必须在当前时间的基础上增加一小时

// You don't have to put System.currentTimeMillis() to the constructor.
// Default constructor of Date gives you the current time.
Date stopTime = new Date();
stopTime.setHours(stopTime.getHours() + 1);
String newStoptime = sdfStopTime.format(stopTime);
stopButton.append(newStoptime);
如果您不想使用不推荐使用的函数
setHours
getHours
, 使用
setTime
getTime
并将3600000毫秒(1小时)添加到当前时间:

stopTime.setTime(stopTime.getTime() + 60 * 60 * 1000);
而不是

stopTime.setHours(stopTime.getHours() + 1);

这使一切保持简单的格式。不需要创建额外的对象

SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a", Locale.ENGLISH);

System.out.println("Before: " + sdfStopTime.getCalendar().getTime());

sdfStopTime.getCalendar().add(Calendar.HOUR, 1);

System.out.println("After: " + sdfStopTime.getCalendar().getTime());
add()方法的第一个参数是字段、小时、分钟等。
第二个参数是要添加的量,如果减法为负数。

这将使所有内容保持SimpleDataFormat格式。不需要创建额外的对象

SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a", Locale.ENGLISH);

System.out.println("Before: " + sdfStopTime.getCalendar().getTime());

sdfStopTime.getCalendar().add(Calendar.HOUR, 1);

System.out.println("After: " + sdfStopTime.getCalendar().getTime());
add()方法的第一个参数是字段、小时、分钟等。
第二个参数是要添加的量,如果减法为负数。

使用
新日期(System.currentTimeMillis())
设置时间的方法当前是使用精确的当前毫秒并从中生成日期。如果您坚持使用毫秒,您需要做的是添加一小时的毫秒或1000*60*60=3600000

因此,您能够满足您需求的方法是使用以下确切代码:

Button stopButton = (Button) findViewById(R.id.StartTrackingEditStopTime_button);

SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a", Locale.ENGLISH);

String newStoptime = sdfStopTime.format(
        new Date(System.currentTimeMillis() + 3600000));

stopButton.setText(newStopTime);
这会奏效的实现这一点的方法2是使用Calendar对象,如果您系统地处理时间,它将非常有用。要执行此操作,请将上面的第三行替换为以下代码:

Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, 1);
Date d = c.getTime();
String newStopTime = sdfStopTime.format(d);

希望这有帮助!你的选择

使用
新日期(System.currentTimeMillis())
设置时间的方法当前是获取准确的当前毫秒并从中生成日期。如果您坚持使用毫秒,您需要做的是添加一小时的毫秒或1000*60*60=3600000

因此,您能够满足您需求的方法是使用以下确切代码:

Button stopButton = (Button) findViewById(R.id.StartTrackingEditStopTime_button);

SimpleDateFormat sdfStopTime = new SimpleDateFormat("hh:mm:ss a", Locale.ENGLISH);

String newStoptime = sdfStopTime.format(
        new Date(System.currentTimeMillis() + 3600000));

stopButton.setText(newStopTime);
这会奏效的实现这一点的方法2是使用Calendar对象,如果您系统地处理时间,它将非常有用。要执行此操作,请将上面的第三行替换为以下代码:

Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR, 1);
Date d = c.getTime();
String newStopTime = sdfStopTime.format(d);

希望这有帮助!你的选择

你好,Heejin,在下面的一行
stopTime.setHours(stopTime.getHours()+1)我有以下警告,使应用程序强制关闭此行的多个标记-不推荐使用类型日期中的getHours()方法-方法setHours(int)从类型开始,日期为deprecated@WillNZ我在回答中添加了更多内容,即使用其他函数而不是不推荐使用的函数。您进行了不必要的计算,并创建了两个不需要的对象。您好,Heejin,在下一行
stopTime.setHours(stopTime.getHours()+1)我有以下警告,使应用程序强制关闭此行的多个标记-不推荐使用类型日期中的getHours()方法-方法setHours(int)从类型开始,日期为deprecated@WillNZ我在回答中补充了更多内容,即使用其他函数而不是不推荐使用的函数。您进行了不必要的计算,并创建了两个不需要的对象。嗨,Jonathan,这就是我在代码中放置此函数的方式,因为它似乎没有进行任何更改`按钮停止按钮=(按钮)this.findViewById R.id.StartTrackingEditStopTime\u按钮);SimpleDataFormat sdfStopTime=新的SimpleDataFormat(“hh:mm:ss a”,Locale.ENGLISH);System.out.println(“Before:+sdfStopTime.getCalendar().getTime());sdfStopTime.getCalendar().add(Calendar.HOUR,10);System.out.println(“之后:”+sdfStopTime.getCalendar().getTime());字符串newStoptime=sdfStopTime.format(新日期(System.currentTimeMillis());stopButton.append(newStoptime);嗨,乔纳森,这就是我在我的代码中放置它的方式,因为它似乎没有做任何更改`按钮停止按钮=(按钮)this.findViewById R.id.StartTrackingEditStopTime\u按钮);SimpleDataFormat sdfStopTime=新的SimpleDataFormat(“hh:mm:ss a”,Locale.ENGLISH);System.out.println(“Before:+sdfStopTime.getCalendar().getTime());sdfStopTime.getCalendar().add(Calendar.HOUR,10);System.out.println(“之后:”+sdfStopTime.getCalendar().getTime());字符串newStoptime=sdfStopTime.format(新日期(System.currentTimeMillis());stopButton.append(newStoptime);谢谢Bepetersn,我已经找到了方法1可以很好地工作,但是使用方法2,我在线路上遇到了一个问题
dated=c.getTime()我得到错误类型不匹配:无法从java.util.Date转换为java.sql.Date。你能帮个忙吗?我发现把上面这行改成这行行行行有效。这是一个好方法吗<代码>java.util.Date d=c.getTime()嘿,我的原始答案中有一个非常严重的错误,我刚刚注意到,在方式1中,我让你把时间改了一整天,而你要求改一小时。我刚修好。至于第二种方式,我对这一点不太确定。我自己也没试过,但从我在文档中看到的情况来看,它应该是这样的。谢谢你的修复。我也注意到了这一点,但在我发现的时候没有提及任何事情。我还注意到,您的newStopTime声明有不同的大小写,这会导致错误,因此您也可以编辑它。非常感谢