apache - Easy way to get a readable date from a long (timestamp) in scala

Apache - Easy way to get a readable date from a long (timestamp) in scala

To convert a long timestamp to a readable date in Scala, you can use the java.time package, specifically Instant and DateTimeFormatter. Here's a simple example:

Example Code

import java.time.Instant import java.time.ZoneId import java.time.format.DateTimeFormatter object TimestampToReadableDate { def main(args: Array[String]): Unit = { val timestamp: Long = 1633036800000L // Example timestamp in milliseconds // Convert timestamp to Instant val instant = Instant.ofEpochMilli(timestamp) // Define the formatter val formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss") // Customize your format .withZone(ZoneId.systemDefault()) // Use the system default time zone // Format the date val readableDate = formatter.format(instant) println(s"Readable date: $readableDate") } } 

Explanation

  1. Instant.ofEpochMilli(timestamp): Converts the timestamp (in milliseconds) to an Instant.
  2. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"): Defines the format of the output date.
  3. withZone(ZoneId.systemDefault()): Ensures the date is formatted in the system's default time zone.
  4. formatter.format(instant): Formats the Instant to a readable date string.

Customization

You can change the date format string in ofPattern to suit your needs (e.g., "dd-MM-yyyy", "MM/dd/yyyy HH:mm").

Examples

  1. Scala convert epoch timestamp to readable date?

    • Description: Shows how to convert an epoch timestamp (in milliseconds) to a readable date format in Scala.
    • Code:
      import java.time.Instant import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val epochMillis = 1626559756000L // Example epoch timestamp val instant = Instant.ofEpochMilli(epochMillis) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"Readable date: $formattedDateTime") } } 
  2. Scala parse long timestamp to human-readable date?

    • Description: Illustrates how to parse a long timestamp into a human-readable date string in Scala using java.time.Instant.
    • Code:
      import java.time.Instant import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val timestamp = 1626559756000L // Example timestamp val instant = Instant.ofEpochMilli(timestamp) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"Human-readable date: $formattedDateTime") } } 
  3. Scala epoch time to readable date string?

    • Description: Provides a method to convert an epoch time (milliseconds) to a formatted readable date string in Scala.
    • Code:
      import java.time.Instant import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val epochTime = 1626559756000L // Example epoch time val instant = Instant.ofEpochMilli(epochTime) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"Readable date: $formattedDateTime") } } 
  4. Scala convert milliseconds to human-readable date?

    • Description: Demonstrates how to convert milliseconds (epoch timestamp) to a human-readable date format using Scala's java.time.Instant.
    • Code:
      import java.time.Instant import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val milliseconds = 1626559756000L // Example milliseconds val instant = Instant.ofEpochMilli(milliseconds) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"Human-readable date: $formattedDateTime") } } 
  5. Scala timestamp to readable date with time zone?

    • Description: Shows how to convert a timestamp to a readable date string including time zone information using Scala's java.time.Instant.
    • Code:
      import java.time.{ Instant, ZoneId } import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val timestamp = 1626559756000L // Example timestamp val instant = Instant.ofEpochMilli(timestamp) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z") .withZone(ZoneId.systemDefault()) val formattedDateTime = formatter.format(instant) println(s"Readable date with time zone: $formattedDateTime") } } 
  6. Scala convert epoch time to string date?

    • Description: Provides a method to convert an epoch time (in milliseconds) to a string representation of a date in Scala.
    • Code:
      import java.time.{ Instant, ZoneId } import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val epochTime = 1626559756000L // Example epoch time val instant = Instant.ofEpochMilli(epochTime) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"String date: $formattedDateTime") } } 
  7. Scala convert timestamp to formatted date?

    • Description: Illustrates how to convert a timestamp (in milliseconds) to a formatted date string using Scala and java.time.Instant.
    • Code:
      import java.time.{ Instant, ZoneId } import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val timestamp = 1626559756000L // Example timestamp val instant = Instant.ofEpochMilli(timestamp) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"Formatted date: $formattedDateTime") } } 
  8. Scala get readable date from epoch milliseconds?

    • Description: Shows how to obtain a readable date from epoch milliseconds using Scala's java.time.Instant and java.time.format.DateTimeFormatter.
    • Code:
      import java.time.Instant import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val epochMillis = 1626559756000L // Example epoch milliseconds val instant = Instant.ofEpochMilli(epochMillis) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"Readable date: $formattedDateTime") } } 
  9. Scala convert epoch milliseconds to formatted date?

    • Description: Provides a method to convert epoch milliseconds to a formatted date string in Scala using java.time.Instant.
    • Code:
      import java.time.Instant import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val epochMillis = 1626559756000L // Example epoch milliseconds val instant = Instant.ofEpochMilli(epochMillis) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"Formatted date: $formattedDateTime") } } 
  10. Scala convert timestamp to human-readable date?

    • Description: Demonstrates how to convert a timestamp to a human-readable date format using Scala and java.time.Instant.
    • Code:
      import java.time.Instant import java.time.format.DateTimeFormatter object TimestampToDate { def main(args: Array[String]): Unit = { val timestamp = 1626559756000L // Example timestamp val instant = Instant.ofEpochMilli(timestamp) val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formattedDateTime = formatter.format(instant) println(s"Human-readable date: $formattedDateTime") } } 

More Tags

fastapi react-component font-size requiredfieldvalidator lombok coordinator-layout django-manage.py sax figures sumoselect.js

More Programming Questions

More Investment Calculators

More Retirement Calculators

More Genetics Calculators

More Bio laboratory Calculators