
String formatted = dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) In Java 8 and later this happens like this: ZonedDateTime dateTime = oldfashionedDateFromLegacyApi.toInstant() If you got a from a legacy API that you don’t want to change just now, convert it to a modern Instant and do further conversions from there. I recommend you avoid them and use the modern API instead. The classes you were using, Date and SimpleDateFormat, are long outdated, and the latter in particular notoriously troublesome. Depending on what you need it for, the result of toString will probably accepted.

#Simpledateformat iso#
Your desired string is in ISO 8601 format, the standard format that the modern classes’ toString methods produce. Using java.time, the modern Java date and time API, in case you just want an utput that conforms with ISO 8601 and don’t care how many decimals (or whether seconds are printed if they are 0), we don’t need an explicit formatter at all. The setting can be changed any time by another part of your program or another program running in the same JVM. Specify ZoneId.systemDefault() for your JVM’s time zone setting. Please substitute your desired time zone if it didn’t happen to be Africa/Cairo. String formatted = now.format(formatterWithThreeDecimals) = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX") To exercise more control over the format, use an explicit formatter: DateTimeFormatter formatterWithThreeDecimals OffsetDateTime now = OffsetDateTime.now(ZoneId.of("Africa/Cairo")) Įdit: I asked for 3 decimals on the seconds, not 6. Used widely in Unix-like and many other operating systems andįile formats.Time for someone to provide the modern answer. (UTC), Thursday, 1 January 1970, not counting leap seconds. That have elapsed since 00:00:00 Coordinated Universal Time Unix time (also known as POSIX time or Epoch time) is a systemįor describing instants in time, defined as the number of seconds
#Simpledateformat how to#
These examples are showing how to parse date in human readable form to unix timestamp in either milliseconds or seconds. String date = dateFormat.format(currentDate) ĭate +"%Y-%m-%d %H:%M:%S" -d Date to Timestamp Examples SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss") These examples are showing how to convert timestamp - either in milliseconds or seconds to human readable form. These examples are showing how to get current date and time that could be presented to the end-user.

Long ts = System.currentTimeMillis()/1000 These examples are returning timestamp in seconds, although some of the languages are returning timestamp in milliseconds.

These examples are showing how to get current unix timestamp in seconds. Or you can construct URL with your timestamp. If you want to convert timestamp, it is sufficient to either enter your timestamp into input area, Timestamp Online is timestamp converver between unix timestamp and human readable form date.
