java的parse方法_Java Duration类| parse()方法与示例

java的parse方法

持续时间类parse()方法 (Duration Class parse() method)

  • parse() method is available in java.time package.

    parse()方法在java.time包中可用。

  • parse() method is used to return a Duration that parses the given char sequence and char sequence follow some ISO -8601 standard format like PnDTnHnMn.nS.

    parse()方法用于返回一个Duration,该Duration解析给定的char序列,并且char序列遵循某些ISO -8601标准格式,例如PnDTnHnMn.nS。

  • parse() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.

    parse()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出错。

  • parse() method may throw an exception at the time of parsing the given sequence.

    parse()方法在解析给定序列时可能会引发异常。

    DateTimeParseException: This exception may throw when the given parameter couldn't parse.

    DateTimeParseException :如果无法解析给定参数,则可能引发此异常。

Syntax:

句法:

    public static Duration parse(CharSequence cs);

Parameter(s):

参数:

  • CharSequence cs – represents the char sequence to parse to a Duration.

    CharSequence cs –表示要解析为Duration的char序列。

Return value:

返回值:

The return type of this method is Duration, it returns the Duration that holds the parse value of the given sequence to a Duration.

此方法的返回类型为Duration ,它将将持有给定序列的解析值的Duration返回为Duration。

Example:

例:

// Java program to demonstrate the example 
// of parse(CharSequence cs) method of Duration

import java.time.*;

public class ParseOfDuration {
    public static void main(String args[]) {
        // parses the given sequence in this Duration du1 
        // i.e. 1D:1H:20M:10S in a standard format PnDTnHnMn.S
        Duration du1 = Duration.parse("P1DT1H20M10S");

        // Display du1
        System.out.println("Duration.parse(P1DT1H20M10S): " + du1);

        // parsees the given sequence in this Duration du1 
        // i.e. 1D:1H: in a standard format PnDTnHnMn.S
        Duration du2 = Duration.parse("P1DT1H");

        // Display du2
        System.out.println("Duration.parse(P1DT1H): " + du2);
    }
}

Output

输出量

Duration.parse(P1DT1H20M10S): PT25H20M10S
Duration.parse(P1DT1H): PT25H


翻译自: https://www.includehelp.com/java/duration-parse-method-with-example.aspx

java的parse方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值