Monday, September 21, 2015

Get current time using java Date class

Creating a Date object with java

When we create a new Date object with default constructor, jvm creates a date object with current time stamp. ( according to local time zone )  

import java.util.Date;
class Demostration{
 public static void main(String[]args){
  Date date=new Date(); // date object
  System.out.println(date); // try to print the date object   
 } 
}






This will give you a output similar to the following

Mon Sep 21 23:29:08 IST 2015


Most of the methods in java.util.Date class are deprecated with the arrival of the java Calendar class, so the next most important method of Date class is the getTime() method.

import java.util.Date;
public class Main {
    public static void main(String[]args){
        Date date =new Date();
        long timeInMilliSeconds=date.getTime();
        System.out.println(timeInMilliSeconds);
    }
}

getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT,
don't worry you'll understand why this is important when you are trying to compare time.

Create a data object with given time (time in milliseconds )

Also you can create a date object providing long argument for the constructor as well.

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

see the output and you'll understand.

As I told you earlier most of the methods of Date class are deprecated now, you can see my other article to learn about Calendar class and how to use both Date and the Calendar.

Your comments and suggestions are welcome.

Monday, September 14, 2015

How to use the Calendar class

Get the current time stamp using java Calendar

import java.util.Calendar;
class Demostration{
 public static void main(String[]args){
  Calendar calendar=Calendar.getInstance();  
  System.out.println(calendar.getTime());    
 } 
}


To work with Calendar class you will need a little knowledge about Date and DateFormat classes, in the following examples I'll show how to use Date and DateFormat classes

Get the current time stamp using java Date


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


By studying above examples you can understand that calendar.getTime() method also returns a new Date class object

Format time stamp using DateFormat, SimpleDateFormat classes


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class Demostration{
 public static void main(String[]args){
  Date date=new Date();
  DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
  String formattedDate=dateFormat.format(date);
  System.out.println(formattedDate);    
 } 
}

in the next example I'm going to add the time also

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class Demostration{
 public static void main(String[]args){
  Date date=new Date();
  DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  String formattedDate=dateFormat.format(date);
  System.out.println(formattedDate);    
 } 
}

Try changing the pattern string ("yyyy-MM-dd hh:mm:ss") to some other like
"EEE-MM-dd hh:mm:ss"