I am generating number from date, I am using Integer.parseInt(myDateNumberString).
My problem is if the number is too large it will give me an error.
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
}
I want to get value like 201812250203
If my date format without mm
it is ok, but I need to declare it.
Use ISO 8601 strings to exchange date-time values, not a number faking as a date-time. Generally best to exchange moments in UTC rather than your own time zone.
Instant
.now()
.truncatedTo( ChronoUnit.MINUTES)
.toString()
2018-12-24T19:32:00Z
To minimize the use of delimiters, use the “basic” variation of ISO 8601 formats. We retain the T
separating the year-month-day portion from the hour-minute portion. Dropping the Z
indicating UTC is ill-advised, but if you insist.
OffsetDateTime
.now( ZoneOffset.UTC )
.format(
DateTimeFormatter.ofPattern( "uuuuMMdd'T'HHmm" )
)
20181224T1936
Using an integer to represent a date-time string is ill-advised, but if you insist.
new BigInteger(
OffsetDateTime
.now( ZoneOffset.UTC )
.format(
DateTimeFormatter.ofPattern( "uuuuMMddHHmm" )
)
)
.toString()
201812241939
A 32-bit integer (int
, Integer
) is limited to 2,147,483,647. Your fake-datetime number of 2,018,123,581,939 is too large for that. You could use a 64-bit integer (long
, Long
). But, again, I must say this is a poor way to represent date-time values.
new BigInteger(
OffsetDateTime
.now( ZoneOffset.UTC )
.format(
DateTimeFormatter.ofPattern( "uuuuMMddHHmm" )
)
)
.longValue()
201812241939
Or, skip BigInteger
and use Long
.
Long
.parseLong(
OffsetDateTime
.now( ZoneOffset.UTC )
.format(
DateTimeFormatter.ofPattern( "uuuuMMddHHmm" )
)
)
201812241939
You are using terrible old date-time classes (SimpleDateFormat
, Calendar
) that were supplanted years ago by the java.time classes with the adoption of JSR 310.
ZonedDateTime
To get the current date and time as seen through the wall-clock time used the people of a particular region, specify a ZoneId
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
DateTimeFormatter
To generate a string in the format of YYYYMMDDHHMM, use DateTimeFormatter
class.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHss" ) ;
String output = zdt.format( f ) ;
I strongly discourage you from trying to represent this value as a number. For date-time values, use date-time objects (java.time objects). For exchanging date-time values as text, generate strings in standard ISO 8601 format. The java.time classes use ISO 8601 formats by default when parsing/generating strings. The ZonedDateTime::toString
method wisely extends the standard by appending the name of the time zone in square brackets.
String output = zdt.toString() ; // Generate text in standard ISO 8601 format wisely extended to append the name of the time zone in square brackets.
But if you insist on doing the number thing, here goes.
BigInteger bigInteger = new BigInteger( zdt.format( f ) ) ;
The 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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The 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.