Java Return 来自字符串的 BigInteger

Java Return BigInteger from String

我正在根据日期生成数字,我正在使用 Integer.parseInt(myDateNumberString)。

我的问题是如果数字太大就会报错。

public Integer currentDate(){
        String current_date = new SimpleDateFormat("yyyyMMddHHmm").format(Calendar.getInstance().getTime());
        //int _current_date = Integer.parseInt(current_date); // Error, too big number
        int _current_date = new BigInteger(current_date).intValue();
        return _current_date; // Error, output: -51212897
    }

我想获得像201812250203这样的价值 如果我的日期格式没有 mm 没问题,但我需要声明它。

您可以使用 BigInteger number = new BigInteger(string);,它将给定的字符串解析为 BigInteger。

docs 说:

Constructs a new BigInteger by parsing value. The string representation consists of an optional plus or minus sign followed by a non-empty sequence of decimal digits. Digits are interpreted as if by Character.digit(char,10).

tl;博士

使用 ISO 8601 字符串交换日期时间值,而不是伪造日期时间的数字。通常最好以 UTC 而非您自己的时区交换时刻。

Instant
.now()
.truncatedTo( ChronoUnit.MINUTES)
.toString()

2018-12-24T19:32:00Z

要尽量减少分隔符的使用,请使用 ISO 8601 格式的“基本”变体。我们保留 T 将年月日部分与小时分钟部分分开。删除表示 UTC 的 Z 是不明智的,但如果你坚持的话。

OffsetDateTime
.now( ZoneOffset.UTC )
.format(
    DateTimeFormatter.ofPattern( "uuuuMMdd'T'HHmm" ) 
)

20181224T1936

使用整数来表示日期时间字符串是不明智的,但如果您坚持的话。

new BigInteger( 
    OffsetDateTime
    .now( ZoneOffset.UTC )
    .format(
        DateTimeFormatter.ofPattern( "uuuuMMddHHmm" ) 
    )
)
.toString()

201812241939

一个 32 位整数(intInteger)限制为 2,147,483,647。您的假日期时间数字 2,018,123,581,939 太大了。您可以使用 64 位整数(longLong)。但是,我必须再次声明,这是表示日期时间值的糟糕方式。

new BigInteger( 
    OffsetDateTime
    .now( ZoneOffset.UTC )
    .format(
        DateTimeFormatter.ofPattern( "uuuuMMddHHmm" ) 
    )
)
.longValue()

201812241939

或者,跳过 BigInteger 并使用 Long

Long
.parseLong(
    OffsetDateTime
    .now( ZoneOffset.UTC )
    .format(
        DateTimeFormatter.ofPattern( "uuuuMMddHHmm" ) 
    )
)

201812241939

java.time

您正在使用可怕的旧日期时间 classes(SimpleDateFormatCalendar),这些日期时间在多年前被 java.time class采用了 JSR 310。

ZonedDateTime

要获取特定地区人们使用挂钟时间看到的当前日期和时间,请指定 ZoneId 以获得 ZonedDateTime

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

DateTimeFormatter

要生成 YYYYMMDDHHMM 格式的字符串,请使用 DateTimeFormatter class。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHss" ) ;
String output = zdt.format( f ) ;

使用适当的数据类型

我强烈建议您不要尝试将此值表示为数字。对于日期时间值,使用日期时间对象(java.time 对象)。要将日期时间值作为文本交换,请生成标准 ISO 8601 格式的字符串。当 parsing/generating 字符串时,java.time classes 默认使用 ISO 8601 格式。 ZonedDateTime::toString 方法通过在方括号中附加时区名称来明智地扩展标准。

String output = zdt.toString() ;  // Generate text in standard ISO 8601 format wisely extended to append the name of the time zone in square brackets.

但是如果你坚持做数字的事情,那就去吧。

BigInteger bigInteger = new BigInteger( zdt.format( f ) ) ;

关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

您可以直接与数据库交换 java.time 对象。使用 JDBC driver compliant with JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* classes.

从哪里获得java.time classes?

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

将方法的 return 类型更改为 long 以及 returning 变量:

编辑:摆脱 .intValue() 方法并将方法的 return 类型更改为 BigInteger 和变量。

public static void main(String[] args) {
    System.out.println(currentDate());
}

public static BigInteger currentDate() {
    String current_date = new SimpleDateFormat("yyyyMMddHHmm").format(Calendar.getInstance().getTime());
    // int _current_date = Integer.parseInt(current_date);
    BigInteger _current_date = new BigInteger(current_date);
    return _current_date;
}