Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 如何更改当前日期_Java_Date - Fatal编程技术网

Java 如何更改当前日期

Java 如何更改当前日期,java,date,Java,Date,我的方法接受以符号/分隔的-小时、分钟、秒和毫秒作为字符串参数 如何将方法中的参数添加到当前日期 示例1:今天,2021年2月10日,该方法接收metnode数据(10/10/10/10)-输出-2021年2月10日10:10:10 示例2:今天,2021年2月10日,该方法接收metnode数据(55/10/10/10)-输出-2021年2月12日07:10:10 也就是说,您需要将55小时10秒10秒10毫秒添加到当前日期 您不能使用Calendar和StringTokenizer类 pub

我的方法接受以符号/分隔的-小时、分钟、秒和毫秒作为字符串参数 如何将方法中的参数添加到当前日期

示例1:今天,2021年2月10日,该方法接收metnode数据(10/10/10/10)-输出-2021年2月10日10:10:10

示例2:今天,2021年2月10日,该方法接收metnode数据(55/10/10/10)-输出-2021年2月12日07:10:10 也就是说,您需要将55小时10秒10秒10毫秒添加到当前日期

您不能使用Calendar和StringTokenizer类

public void method(String s) {

        s = s.replaceAll("/", "-");

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss");

        final LocalDateTime now = LocalDateTime.parse(s, formatter.withResolverStyle(ResolverStyle.LENIENT));

        System.out.println(now);
    }
我找到了withResolverStyle(ResolverStyle.LENIENT)方法 但是我不知道如何使用它。

仔细查看。它提供了多种组合日期的方法。例如:


为了简单起见,您可以使用LocalDateTime类。这很容易理解。请参阅以下代码,该代码用于将小时、分钟、秒和毫微秒添加到当前日期时间。 然后可以根据需要通过任何格式模式轻松格式化此日期时间

  public void addDateTime(int hours, int minuts, int seconds, int nanos) {
        LocalDateTime adt = LocalDateTime.now();
        System.out.println(adt);
        adt = adt.plusHours(hours);
        adt = adt.plusMinutes(minuts);
        adt = adt.plusSeconds(seconds);
        adt = adt.plusNanos(nanos);
        System.out.println(adt);
        
    }
试试这个:

public void method(String s) {
    String[] arr = s.split("/");
    LocalDateTime now = LocalDateTime.of(
        LocalDate.now(), LocalTime.of(0, 0))
        .plusHours(Integer.parseInt(arr[0]))
        .plusMinutes(Integer.parseInt(arr[1]))
        .plusSeconds(Integer.parseInt(arr[2]))
        .plusNanos(Integer.parseInt(arr[3]) * 1_000_000L);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");
    System.out.println(now.format(formatter));
}
一个宽松的
DateTimeFormatter
就足够了 我不知道这是不是最好的解决办法。这可能取决于口味。它确实使用了您提到的
ResolverStyle.LENIENT
,并且通常按照您的问题中的代码行工作,只是进行了固定和稍微简化

我的格式化程序包括日期和时间。这是将剩余小时转换为天所必需的

private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .appendPattern("uuuu-MM-dd H/m/s/")
        .appendValue(ChronoField.MILLI_OF_SECOND)
        .toFormatter()
        .withResolverStyle(ResolverStyle.LENIENT);
接下来我们需要一个与格式化程序匹配的字符串。因此,让我们将日期前置到我们已经得到的时间字符串:

    String timeString = "55/10/10/10";
    
    LocalDate today = LocalDate.now(ZoneId.of("America/Regina"));
    String dateTimeString = "" + today + ' ' + timeString;
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);

    System.out.println(dateTime);
我今天(2月10日)运行代码时的输出是:

2021-02-12T07:10:10.010

另一个想法:使用
持续时间

编辑:另一种方法是使用
Duration
类。这样做的一个原因是,看起来您实际上是在添加持续时间,而不是设置一天中的时间。一个缺点是将字符串解析为
持续时间
有点棘手。我们想要使用的
Duration.parse
方法只接受ISO 8601格式。在55小时10分钟10.010秒的时间段内,它像
PT55H10M10.010S
。是的,毫秒需要以秒的分数来表示

    String isoTimeString = timeString.replaceFirst("(/)(\\d+)$", "$100$2")
            .replaceFirst("(\\d+)/(\\d+)/(\\d+)/0*(\\d{3})", "PT$1H$2M$3.$4S");
    Duration dur = Duration.parse(isoTimeString);
    LocalDateTime dateTime = LocalDate.now(ZoneId.of("Asia/Kathmandu"))
            .atStartOfDay()
            .plus(dur);
当我刚刚运行它时,2月11日已经在尼泊尔加德满都运行了,结果是:

2021-02-13T07:10:10.010

我使用了两个调用
replaceFirst()
,每次都使用正则表达式。第一个调用只是将一些前导零添加到毫秒<替换字符串中的code>$1
$2
给出了正则表达式中用圆括号表示的第一组和第二组所匹配的内容

第二个
replaceFirst()
调用建立了ISO 8601格式,其中包括确保毫秒正好是三位数字,以便它们作为秒的小数部分工作

    String isoTimeString = timeString.replaceFirst("(/)(\\d+)$", "$100$2")
            .replaceFirst("(\\d+)/(\\d+)/(\\d+)/0*(\\d{3})", "PT$1H$2M$3.$4S");
    Duration dur = Duration.parse(isoTimeString);
    LocalDateTime dateTime = LocalDate.now(ZoneId.of("Asia/Kathmandu"))
            .atStartOfDay()
            .plus(dur);

链接:

谢谢你的好建议时间可能超过24小时,没关系。毫秒可以大于999吗?对于添加毫秒,我会发现以下方法更好:
.plus(Integer.parseInt(arr[3]),ChronoUnit.MILLIS)