Convert BigInt timestamp to Date in java? [duplicate] - java

This question already has answers here:
How to convert currentTimeMillis to a date in Java?
(13 answers)
Closed 5 years ago.
I did some searched on web about BigInt data conversion into date while I've found plenty of same question but non of them seems to work. There is 13 digit's data 1435555326831 into my database and I think this is UNIXTIME, now I want to convert it into yyyy-MM-dd HH:mm:ss form. Thank you

You can first convert the number into a long (if you receive a BigInteger, you can call BigInteger.longValue()).
Then you have two options. With the standard java.util.Date you can use:
long millis = 1435555326831L;
Date d = new Date(millis);
You can then format the date with a SimpleDateFormat for output.
If you can use Java 8's new Time API, you can create an instant and convert it to the desired time zone (your computer time zone in my example below):
Instant instant = Instant.ofEpochMilli(millis);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(fmt.format(instant.atZone(ZoneId.systemDefault())));
Note that these conversions only work if BigInteger is smaller than the maximum long size, not in general. This shouldn't be an issue, since the maximum value of a long is 2^63 - 1, but if your BigInteger is user input, you need to check for this.

Your data is on Unix timestamp and you can simply convert it by using new java.util.Date()
and here is the example of it
Java: Date from unix timestamp

Related

How to modify decimal fractions from UTC Date Time to Date [duplicate]

This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Date object SimpleDateFormat not parsing timestamp string correctly in Java (Android) environment
(7 answers)
Closed 2 years ago.
I'm trying to convert a utc date time in local date time, but I have some problem the the decimal fraction.
I call a web service the return a series of data. One of these data is the utc date time in this format
I must the use this library org.threeten.bp, I can't use a different library.
2020-06-22T18:28:57.957535800Z
To converte utcFormat to Date,I have found this piece of code that it works fine
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = utcFormat.parse("2012-08-15T22:56:02.038Z");
DateFormat pstFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
pstFormat.setTimeZone(TimeZone.getTimeZone("ECT"));
System.out.println(pstFormat.format(date));
but it doesen't work well from my code, because return this date
2020-07-03T22:27:52.800
How you can see it's different. I did some test and if I leave only 3 decimal after dot, that part of code it will work fine. Have a look the example:
2020-06-22T18:28:57.800Z
return the right date time from ECT zone
2020-06-22T20:28:57.800
I am looking for a way to receive the utc dateTime with only three decimals or to change the utc dateTime by removing the excess decimals. With this last case I am not if it can be a good idea.
Your input format is standard so you can simply parse it to an Instant for example:
String input = "2020-06-22T18:28:57.957535800Z";
Instant date = Instant.parse(input);
If you want to get rid of the last 3 decimals (i.e. only keep the result to a microsecond precision), you can truncate the result:
Instant truncated = date.truncatedTo(ChronoUnit.MICROS);
Also note that the classes you use in your example (DateFormat, Date etc) are not part of threeten.
Here's an approach similar to yours but using classes from org.threeten.bp only instead of mixing it with java.util:
public static void main(String[] args) throws ParseException {
String datetimeUtc = "2020-06-22T18:28:57.957535800Z";
// parse it to a ZonedDateTime, this is default formatting ==> no formatter needed
ZonedDateTime utcTime = ZonedDateTime.parse(datetimeUtc);
// print the result
System.out.println(utcTime);
// convert it to another zone
ZonedDateTime estTime = utcTime.withZoneSameInstant(ZoneId.of("Europe/Paris"));
// print that, too
System.out.println(estTime);
// define a non-default output format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
// and print the estTime using that format
System.out.println(estTime.format(dtf));
}
This outputs the following:
2020-06-22T18:28:57.957535800Z
2020-06-22T20:28:57.957535800+02:00[Europe/Paris]
2020-06-22T20:28:57.957

Getting a time difference in Java LocalDateTime and converting it to String format [duplicate]

This question already has answers here:
How to format a duration in java? (e.g format H:MM:SS)
(22 answers)
Closed 3 years ago.
I'm taking a time difference between two LocalDateTime values. I'm using the Duration for that. And now I want to convert it into hh:mm:ss format as a String value. I've been checking how this can be done but still no luck. Any guidance will be appreciated.
LocalDateTime startTime = some value
LocalDateTime endTime = some value
Duration totalWaitingDuration = Duration.between(endTime, startTime);
String waitingTime = String.valueOf(totalWaitingDuration.toHours());
In this, I'm getting the time difference only in hours. How can i get it in hh:mm:ss format?
You can use apache commons:
DurationFormatUtils.formatDuration(totalWaitingDuration.toMillis(), "H:mm:ss", true)
Duration must not be negative.

19 digit timestamp conversion in java [duplicate]

This question already has answers here:
long timestamp to LocalDateTime
(6 answers)
Java Converting 19-digit Unix Timestamp to a Readable Date
(3 answers)
Closed 4 years ago.
Is there any library i can use in java to properly convert 19 digit unix timestamp in the proper human readable date format in java ?
Eg:
1547111550416874183
1547111550917748553
You've got a timestamp in nanoseconds there, by the looks of it. (If it's not in nanos, adjust the 1_000_000_000 accordingly).
Split it into seconds and nanos:
long seconds = timestamp / 1_000_000_000;
long nanos = timestamp % 1_000_000_000;
Then construct a java.time.Instant:
Instant instant = Instant.ofEpochSecond(seconds, nanos);
Then you've got the whole java.time API available to do whatever you need to with it.
You can use this :
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy 'at' h:mm a");
String date = sdf.format(myTimestamp);
Hope it helps !

Java DateTime viewing [duplicate]

This question already has answers here:
String -> java.util.Date -> java.sql.Date (with time stamp)
(3 answers)
Closed 6 years ago.
Hello i'm defining a variable in which i want to stock date-time input.
I gave this variable a format which is yyyy-mm-dd hh:MM.
But in the database it keeps showing me only this format yyyy-mm-dd without hh:MM
The code
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd hh:MM");
java.util.Date date = sdf1.parse(rs.getString("dateMaint"));
java.sql.Date sqlStartDate = new Date(date.getTime());
mc.setDateMaint(sqlStartDate );
Also check if the database data type for your data is correct. This is dabase vendor dependant, for example an Oracle date type contains both time and date, but MS SQL server date means only date part: https://msdn.microsoft.com/en-us/library/bb630352.aspx
public static java.sql.Timestamp convertToSqlDateTime(Date utilDate){
return new java.sql.Timestamp(utilDate.getTime());
}
Normally, java.sql.Date only returns a date-only value and time will be discarded. So, in order to get time also, java.sql.TimeStamp must be used.
TimeStamp constructs a Timestamp object using a milliseconds time value. The integral seconds are stored in the underlying date value; the fractional seconds are stored in the nanos field of the Timestamp object.
For this purpose, utilDate.getTime() is used to return the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date Object.
For more information link: Source

Date with Time zone in Java [duplicate]

This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
My requirement is to have "YYYYMMDDHHMMSS.XXX [gmt offset[:tz name]]" date format (ex: 20140425053117.694[+5.30:IST]) for a Date field in Java.
I can achieve this simply using this SimpleDateFormat class which returns a String as output. But I want the output as a "Date" object with the above Pattern.
How can I achieve this, please help!!
The java.util.Date object encapsulates a long value that represents number of milliseconds since an epoch. In simple terms, you can think of Date class as a convenient way of storing a long number representing a date along with timezone information.
Whenever you want to display the date, you can format it any way, for example with SimpleDateFormat.
Prior to JDK 1.1, the java.util.Date could be used to parse and format dates. Starting from JDK 1.1 the parsing and formatting related methods of java.util.Date are deprecated.
Read more at Date class' javadoc page
I think it's what you want:
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = isoFormat.parse("2010-05-23T09:01:02");

Categories