以时间格式时移作为输入,平移后的实时作为输出

发布于 2025-01-07 13:34:21 字数 141 浏览 0 评论 0原文

请问,我该如何:

  • 输入(不使用时间选择器)时间偏移,例如 02:21:22,
  • 然后显示添加了时间偏移的时钟。

因此,如果当前时间为 13:20:31,则显示的时间将为 15:41:53 ...

Please, how could I:

  • to input (not using timepicker) a time shift, for example as 02:21:22
  • then to display a clock with the time shift added to it.

So if the time is currently 13:20:31 the displayed time would be 15:41:53 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2025-01-14 13:34:21

您可能需要一些类似 Calendar< /code>用于时间添加。示例:

Calendar future = Calendar.getInstance();
future.add(Calendar.HOUR_OF_DAY, enteredHour);
future.add(Calendar.MINUTE, enteredMinute);
future.add(Calendar.SECOND, enteredSecond);
//And so on...
//Now the calendar instance 'future' holds the current time plus the added values.

对于输入时间,一种可能是将其作为文本输入,然后使用 DateFormatter 获取 日期 来自它。例子:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date enteredDate = sdf.parse(string); //String can be the input from an edit-text for example.
//You can now get the hours/minutes/seconds from the date with any of the Dates get-methods.

You will probably want some thing like the Calendar for the time addition. Example:

Calendar future = Calendar.getInstance();
future.add(Calendar.HOUR_OF_DAY, enteredHour);
future.add(Calendar.MINUTE, enteredMinute);
future.add(Calendar.SECOND, enteredSecond);
//And so on...
//Now the calendar instance 'future' holds the current time plus the added values.

As for entering the time, one possibility is to enter it as a text, and then use a DateFormatter to get a Date from it. Example:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date enteredDate = sdf.parse(string); //String can be the input from an edit-text for example.
//You can now get the hours/minutes/seconds from the date with any of the Dates get-methods.
套路撩心 2025-01-14 13:34:21
/**
 * This function returns the formatted string of current time, shifted by time interval, </br>
 * set in "shift" parameter, formatted in the same way.
 * @param shift
 * @return
 * @throws ParseException
 */
static String addToCurrent(String shift) throws ParseException {
    long now=System.currentTimeMillis();
    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
    Date shiftDate=df.parse(shift);
    long shiftedLong= shiftDate.getTime()+now;
    return df.format(shiftedLong);
}
/**
 * This function returns the formatted string of current time, shifted by time interval, </br>
 * set in "shift" parameter, formatted in the same way.
 * @param shift
 * @return
 * @throws ParseException
 */
static String addToCurrent(String shift) throws ParseException {
    long now=System.currentTimeMillis();
    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
    Date shiftDate=df.parse(shift);
    long shiftedLong= shiftDate.getTime()+now;
    return df.format(shiftedLong);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文