Converting date value

In this chapter you will learn:

  1. Convert date value to long value in milliseconds
  2. Convert Date value to String

Convert date value to long value in milliseconds

  • long getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
  • void setTime(long time) Sets to represent time that is time milliseconds after January 1, 1970 00:00:00 GMT.

The following code converts Date into milliseconds.

import java.util.Date;
//  j a  v a 2  s . c  o  m
public class Main {
  public static void main(String args[]) {
 
    Date date = new Date();
    System.out.println("Date is : " + date);
 
    System.out.println("Milliseconds since January 1, 1970, 00:00:00 GMT : "
        + date.getTime());
  }
}

The output:

Convert Date value to String

String toString() converts this Date object to a String of the form:

import java.util.Date;
/* j av a  2 s  .  co m*/
public class Main {
  public static void main(String args[]) {
 
    Date date = new Date();
    System.out.println("Date is : " + date.toString());
 
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Create DateFormat to format date