Thursday, October 15, 2015

Java EPOCH to Date Time and vice verse converter







package DateTime;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class DateExample {

 public static void main(String[] args) {
  ChangeDate dateuse = new ChangeDate();
  dateuse.EpochToDateTime();
  dateuse.dateTimeToEpoch();

 }

}

class ChangeDate {

 String formatDate;

 public void EpochToDateTime() {
  DateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
  format.setTimeZone(TimeZone.getDefault());
  formatDate = format.format(new Date().getTime());
  System.out.println(formatDate);
  System.out.println("epoch " + new Date().getTime());
  // System.out.println(System.currentTimeMillis());
 }

 public void dateTimeToEpoch() {
  // String dateTime = "20/11/2015 18:55:00";
  SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss.SSS");
  // format.setTimeZone(TimeZone.getDefault());
  try {
   Date epoch = format.parse(formatDate);
   System.out.println(epoch.getTime());

  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}


No comments:

Post a Comment