Java 两小时之和

Java 两小时之和,java,datetime,Java,Datetime,我正在开发一个应用程序,我想增加小时。但我不知道该怎么做才能考虑到一天的变化。如果我有 晚上9:45+3:30 它应该给 凌晨1:15 感谢您的帮助当您想在任何地方拨打此电话时,这里有一个决定: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TimeClass { sta

我正在开发一个应用程序,我想增加小时。但我不知道该怎么做才能考虑到一天的变化。如果我有

晚上9:45+3:30

它应该给

凌晨1:15


感谢您的帮助

当您想在任何地方拨打此电话时,这里有一个决定:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeClass {

    static String timeStart24 = "21:45";
    static String timeStart = "09:45 PM";
    static String timeStep = "3:30";

    public String TimeClass(String start, String step) throws ParseException {
        // Take hours and minutes apart
        String[] time = step.split(":");
        // Create format of time
        SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
        SimpleDateFormat df24 = new SimpleDateFormat("HH:mm");
        // Input begining time
        Date from = df.parse(start);
        System.out.println(df.format(from));
        System.out.println(df24.format(from) + " - 24 hours format");

        // Create calendar instance
        Calendar cal = Calendar.getInstance();
        cal.setTime(from);
        // Inner method add of Calendar
        cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
        cal.add(Calendar.MINUTE, Integer.parseInt(time[1]));

        System.out.println(df.format(cal.getTime()));
//            System.out.print(df24.format(cal.getTime()));
        return df.format(cal.getTime());
    }

    public static void main(String[] args) throws ParseException {
        TimeClass tc = new TimeClass();
        tc.TimeClass(timeStart, timeStep);
    }

}
输出

09:45 PM
21:45 - 24 hours format
01:15 AM

当你想在任何地方打电话时,这里有一个决定:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeClass {

    static String timeStart24 = "21:45";
    static String timeStart = "09:45 PM";
    static String timeStep = "3:30";

    public String TimeClass(String start, String step) throws ParseException {
        // Take hours and minutes apart
        String[] time = step.split(":");
        // Create format of time
        SimpleDateFormat df = new SimpleDateFormat("hh:mm a");
        SimpleDateFormat df24 = new SimpleDateFormat("HH:mm");
        // Input begining time
        Date from = df.parse(start);
        System.out.println(df.format(from));
        System.out.println(df24.format(from) + " - 24 hours format");

        // Create calendar instance
        Calendar cal = Calendar.getInstance();
        cal.setTime(from);
        // Inner method add of Calendar
        cal.add(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]));
        cal.add(Calendar.MINUTE, Integer.parseInt(time[1]));

        System.out.println(df.format(cal.getTime()));
//            System.out.print(df24.format(cal.getTime()));
        return df.format(cal.getTime());
    }

    public static void main(String[] args) throws ParseException {
        TimeClass tc = new TimeClass();
        tc.TimeClass(timeStart, timeStep);
    }

}
输出

09:45 PM
21:45 - 24 hours format
01:15 AM
它将于下午5:30打印

编辑:一天中的一小时提供24小时

import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;

class SumHours{

    public static void main(String[] args){

        DateFormat dateFormat = new SimpleDateFormat("h:mm a");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY,21);
        cal.set(Calendar.MINUTE,45);

        Date d = cal.getTime();
        System.out.println(dateFormat.format(d));

        cal.add(Calendar.HOUR, 3);
        cal.add(Calendar.MINUTE, 30);
        d = cal.getTime();

        System.out.println(dateFormat.format(d));
    }
}
它将于下午5:30打印

编辑:一天中的一小时提供24小时

import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;

class SumHours{

    public static void main(String[] args){

        DateFormat dateFormat = new SimpleDateFormat("h:mm a");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY,21);
        cal.set(Calendar.MINUTE,45);

        Date d = cal.getTime();
        System.out.println(dateFormat.format(d));

        cal.add(Calendar.HOUR, 3);
        cal.add(Calendar.MINUTE, 30);
        d = cal.getTime();

        System.out.println(dateFormat.format(d));
    }
}
输出:

9:45 PM
1:15 AM
输出:

9:45 PM
1:15 AM

既然这么多人都想为这个重复的问题提供一个答案(在我看来),我想是时候有人提供一个现代的答案了

我知道您使用的是Android Java 7,在Java 8进入Android之前,现代的答案要求您使用外部库ThreeTenABP。然而,不仅该库中较新的Java日期和时间类更易于使用,在时间算法方面,这也是它们的一个特别优点。所以想一想,试试看。这也是未来的趋势,因为这些类内置于Java8和更高版本中

    DateTimeFormatter timeFormatter
            = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
    LocalTime startTime = LocalTime.of(21, 45);
    Duration hoursToAdd = Duration.ofHours(3).plusMinutes(30);
    LocalTime resultTime = startTime.plus(hoursToAdd);

    System.out.println("" + startTime.format(timeFormatter) + " + " + hoursToAdd 
            + " = " + resultTime.format(timeFormatter));
这张照片是:

9:45 PM + PT3H30M = 1:15 AM
我本来想给你的问题中的小写字母
pm
am
3:30
。我承认我们还不太了解。特别是
PT3H30M
如果您还没有学习ISO 8601语法,那么它就很特别。这意味着只有3小时30分钟,当你知道的时候就足够简单了<代码>持续时间对象本身不适合格式化,这在Java9中会有所帮助,但只要Java8还没有出现在Android上,我们就不谈了。如果您更喜欢小写的
pm
,您可以在中找到解决方案

我的代码可能不会比其他答案中的代码短那么多,但我认为它更容易阅读

进一步链接

    • 既然这么多人都想为这个重复的问题提供答案(我认为是这样),我想是时候有人提供现代答案了

      我知道您使用的是Android Java 7,在Java 8进入Android之前,现代的答案要求您使用外部库ThreeTenABP。然而,不仅该库中较新的Java日期和时间类更易于使用,在时间算法方面,这也是它们的一个特别优点。所以想一想,试试看。这也是未来的趋势,因为这些类内置于Java8和更高版本中

          DateTimeFormatter timeFormatter
                  = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
          LocalTime startTime = LocalTime.of(21, 45);
          Duration hoursToAdd = Duration.ofHours(3).plusMinutes(30);
          LocalTime resultTime = startTime.plus(hoursToAdd);
      
          System.out.println("" + startTime.format(timeFormatter) + " + " + hoursToAdd 
                  + " = " + resultTime.format(timeFormatter));
      
      这张照片是:

      9:45 PM + PT3H30M = 1:15 AM
      
      我本来想给你的问题中的小写字母
      pm
      am
      3:30
      。我承认我们还不太了解。特别是
      PT3H30M
      如果您还没有学习ISO 8601语法,那么它就很特别。这意味着只有3小时30分钟,当你知道的时候就足够简单了<代码>持续时间对象本身不适合格式化,这在Java9中会有所帮助,但只要Java8还没有出现在Android上,我们就不谈了。如果您更喜欢小写的
      pm
      ,您可以在中找到解决方案

      我的代码可能不会比其他答案中的代码短那么多,但我认为它更容易阅读

      进一步链接


      哪个Java版本?实际上是AndroidStudio 2.3这是一个很好的例子:@VasylLyashkevych谢谢,我很快会看一看并告诉你Android-所以假设Java 7@ManuelSchmidtWhich Java版本?事实上是AndroidStudio 2.3这是一个很好的例子:@VasylLyashkevych谢谢,我很快会看一看,然后会告诉你Android-假设Java 7@Manuelschmidts可以添加参数来管理24小时系统,好吗?可以添加参数来管理24小时系统吗?当你想使用24小时格式时,你需要使用:SimpleDateFormat df=new SimpleDateFormat(“HH:mm”);谢谢我找到了另一个解决方案并编辑了@Avijit Karmakar answer(等待同行评审),当您想要使用24小时格式时,您需要使用:SimpleDateFormat df=new SimpleDateFormat(“HH:mm”);谢谢我找到了另一个解决方案并编辑了@Avijit Karmakar answer(等待同行评审)来格式化
      持续时间
      您可以使用apache commons lang(我使用版本2.6)中的
      org.apache.commons.lang.time.DurationFormatUtils
      DurationFormatUtils.formatDuration(Duration.toMillis(),“H:mm”)
      outputs
      3:30
      要格式化
      Duration
      您可以使用apache commons lang(我使用版本2.6)中的
      org.apache.commons.lang.time.DurationFormatUtils
      输出
      Duration formatUtils.formatDuration(Duration.toMillis(),“H:mm”)
      outputs
      3:30