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 !
Related
This question already has answers here:
How to convert 24 hr format time in to 12 hr Format?
(16 answers)
Closed 2 years ago.
I have a time in the format HHMM (1643) and I want it to be formatted to HH:MM AM/PM (04:43 PM)
How can I achieve this in Java?
I'm doing something similar for the date which is formatted YYYMMDD (20201013) into MM/DD/YYY (10/13/2020) with this code:
new java.text.SimpleDateFormat("yyyyMMdd").parse($F{date})
You can use DateTimeFormatter & LocalTime from java.time
First, parse the time as LocalTime using DateTimeFormatter. Then again format the LocalTime into String in your desire format.
LocalTime time = LocalTime.parse("1643", DateTimeFormatter.ofPattern("HHmm"));
String formattedTime = time.format(DateTimeFormatter.ofPattern("hh:mm a"));
System.err.println(formattedTime);
Output: 04:43 PM
This question already has answers here:
Java format hour and min
(4 answers)
Convert number of seconds into HH:MM (without seconds) in Java
(3 answers)
Calculating the difference between two Java date instances
(45 answers)
How to format a duration in java? (e.g format H:MM:SS)
(22 answers)
Closed 4 years ago.
I'm trying to countdown to a time in the future (2024) and I have the time left from the users system to the exact date in 2024.
String input = "Mon Apr 08 2024 18:18:29 UTC";
Date date = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH).parse(input);
long milliseconds = date.getTime();
long millisecondsFromNow = milliseconds - (new Date()).getTime();
That is my code, im trying to turn millisecondsFromNowinto a date a human can read, more specifically, years, months, days, hours, minutes, seconds and milliseconds left until the date.
You can create a Duration object using the ofMillis method. Then have a look into this question for formatting options.
This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 5 years ago.
How can I convert minutes from Unix timestamp to date and time in java? For example, timestamp 1372339860 correspond to Thu, 27 Jun 2013 13:31:00 GMT.
I want to convert 1372339860 to 2013-06-27 13:31:00 GMT.
Edit: Actually I want it to be according to US timing GMT-4, so it will be 2013-06-27 09:31:00.
You can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
The pattern that SimpleDateFormat takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Because a Date provides a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)
Java 8 introduces the Instant.ofEpochSecond utility method for creating an Instant from a Unix timestamp, this can then be converted into a ZonedDateTime and finally formatted, e.g.:
final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final long unixTime = 1372339860;
final String formattedDtm = Instant.ofEpochSecond(unixTime)
.atZone(ZoneId.of("GMT-4"))
.format(formatter);
System.out.println(formattedDtm); // => '2013-06-27 09:31:00'
I thought this might be useful for people who are using Java 8.
You need to convert it to milliseconds by multiplying the timestamp by 1000:
java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);
This question already has answers here:
convert String in time to Time object without Date
(3 answers)
Closed 6 years ago.
I am trying to convert a 6 digits String to Time without the date with SimpleDateFormat but I am getting the date of 01.01.1970 after converting the time. How can I just get the time stored in the time variable without the date?
Code
String timeString = "004500";
SimpleDateFormat formater = new SimpleDateFormat("hhmmss");
Date time= formater.parse(timeString );
you can use LocalTime which is what you want. You can parse a standard date like this:
String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
LocalTime time - dateTime.toLocalTime();
If you only have a time to parse you can use LocalTime.parse() method.
Note that this is only available starting from Java 8. You can also add Joda Time as a dependency if you are not using Java 8 yet.
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