Epoch Converter and Date/Time in Java

Quick Navigation

Date and Time

An easy way to get the current date and time in Java is using the Date class which is available in the java.util package.



    import java.util.Date;

    class Main {
      public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date);
      }
    }

                

Output:



    Sat Feb 16 03:50:30 UTC 2019

                


Formatting Dates

There are multiple ways to format dates.



    import java.util.*;
    import java.text.*;

    class Main {
      public static void main(String[] args) {
        //1st way
        System.out.println();
        Date date = new Date();
        System.out.printf("%1$s %2$tB %2$td, %2$tY", "Current date:", date);

      //2nd way
        System.out.println("\n");
        Date d = new Date();
        SimpleDateFormat ft =
        new SimpleDateFormat ("E yyyy-MM-dd 'at' hh:mm:ss a zzz");
        System.out.println("Current Date and Time: " + ft.format(d));

      //3rd way
        System.out.println();
        Date d2 = new Date();
        // display time and date
        String str = String.format("Current Date and Time: %tc", d2 );
        System.out.printf(str);
      }
    }

                

Output:



    Current date: February 16, 2019

    Current Date and Time: Sat 2019-02-16 at 03:52:46 AM UTC

    Current Date and Time: Sat Feb 16 03:52:46 UTC 2019

                

Click here to learn more about formatting dates.


Convert from Epoch to Human Readable Date

Here is the way in Java to convert epoch or unix time to a human readable date.



    import java.util.*;

    class Main {
      public static void main(String[] args) {
          //You could start with a string representation of epoch
          String epochString = "1549499024";
          long epoch = Long.parseLong( epochString );
          System.out.println("Convert Epoch " + epoch + " to date: ");
          Date d = new Date( epoch * 1000 ); //convert epoch seconds to microseconds
          System.out.println(d);

          //You could start with a long number epoch
          long epoch2 = 1550544173;
          System.out.println(new Date(epoch2 * 1000));
      }
    }

                

Output:



    Convert Epoch 1549499024 to date:
    Thu Feb 07 00:23:44 UTC 2019
    Tue Feb 19 02:42:53 UTC 2019

                


Convert from Human Readable Date to Epoch

Here is the way in Java to format the date to show as epoch or unix time.



    import java.util.*;

    class Main {
      public static void main(String[] args) {
          //Get the unix timestamp using Date
          Date currentDate = new Date();
          long epoch = currentDate.getTime() / 1000;
          System.out.println("Epoch: " + epoch);

          //You could use string format to format to unix timestamp
          Date date = new Date();
          String str = String.format("Epoch using string format: %ts", date );
          System.out.printf(str);
      }
    }

                

Output:



    Epoch: 1550544489
    Epoch using string format: 1550544489

                


Programming Languages: