·  JAVA – Syntax – Variables – Date/Time

 

- Info:

  - This tutorial shows how to work with Date & Time in Java.

  - This tutorial is based on http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html.

 

- Calendar:

  - Create JAVA Console Application Test.java

 

    Test.java

    import java.util.Calendar;

   

    public class Test {

     

      public static void main(String[] args) {  

        

        //GET CURRENT DATE.

        Calendar  calendar = Calendar.getInstance(); //Get current date.

                  calendar.set(2012,1,13);           //Set date. year,month,dayofmonth

                   calendar.set(2012,1,13, 23,45,12); //year,month,dayofmonth, hours24,minutes,seconds

                  calendar.clear();

         

        //GET DATE COMPONENTS.

        int year        = calendar.get(Calendar.YEAR        ); //2012

        int month       = calendar.get(Calendar.MONTH       ); //1     [0, 12]

        int dayOfMWeek  = calendar.get(Calendar.DAY_OF_WEEK ); //1     [1-Sunday,...,7-Suterday]

        int dayOfMonth  = calendar.get(Calendar.DAY_OF_MONTH); //13    [1, 31]  

        int hour12      = calendar.get(Calendar.HOUR        ); //11    [0, 11]

        int hour24      = calendar.get(Calendar.HOUR_OF_DAY ); //23    [0, 23]

        int minutes     = calendar.get(Calendar.MINUTE      ); //45    [0, 59]

        int seconds     = calendar.get(Calendar.SECOND      ); //12    [0, 59]

        int miliseconds = calendar.get(Calendar.MILLISECOND ); //312   [0, 999]

            

        //CALCULATE.   

        calendar.add(Calendar.YEAR        ,  1);               //2013  Add        1 year.   Next year 

        calendar.add(Calendar.MONTH       ,  2);               //3     Add        2 months.

        calendar.add(Calendar.DAY_OF_MONTH, -1);               //12    Substract  1 month.  Yesterday.        calendar.add(Calendar.HOUR        , -1);               //22    Substract  1 hour.

        calendar.add(Calendar.MINUTE      , 20);               //5     Add       20 minutes.

        calendar.add(Calendar.SECOND      , 20);               //32    Add       20 seconds.

        calendar.add(Calendar.MILLISECOND , 20);               //32    Add       20 miliseconds.

                     

        //DISPLAY.

        System.out.println("year       ="+year       );

        System.out.println("month      ="+month      );

        System.out.println("dayOfMonth ="+dayOfMonth );

        System.out.println("dayOfMWeek ="+dayOfMWeek );

        System.out.println("hour12     ="+hour12     );

        System.out.println("hour24     ="+hour24     );

        System.out.println("minutes    ="+minutes    );

        System.out.println("seconds    ="+seconds    );

        System.out.println("miliseconds="+miliseconds);

   

      }

   

    }


- Date:

  - Date class is depricated and should be replaced with Calendar.

  - Create JAVA Console Application Test.java

 

    Test.java

    import java.util.Date;

    import java.text.SimpleDateFormat;

   

    public class Test {

   

      public static void main(String[] args) {

   

        //MILISECONDS.

        long   miliSecs = System.currentTimeMillis(); //Current time in miliseconds.

        System.out.println(miliSecs);                 //1107879156077

        

        //DISPLAY COMPONENTS OF DATE & TIME.

        Date   date              = new Date           ();   //Current date and time.

        String text              = date.toLocaleString();   //Tue Feb 08 17:04:07 CET 2005

        int    dayOfWeek         = date.getDay        ();   //0-Sunday,...,6-Suterday  Day of week.

        int    dayOfMonth        = date.getDate       ();   //[1,31]  Day of the

        int    month             = date.getMonth      ();   //[0,11]

        int    year              = date.getYear  ()+1900;   //2012

        int    hours             = date.getHours      ();   //[0,23]

        int    monutes           = date.getMinutes    ();   //34

        int    seconds           = date.getSeconds    ();   //12

        long   yearInMiliseconds = date.getTime       ();   //1107879156077 current time in miliseconds.        

        //CREATE DATE OBJECT FROM.

        Date date1 = new Date();                      //Current date

        Date date2 = new Date(miliSecs);              //miliseconds

        Date date3 = new Date(105, 0, 12);            //(year,month,day) year=wanted-1900 month=[0,11]        Date date4 = new Date(105, 02, 12, 12, 59, 1);//(year,moth,day,hrs,min,sec) integers day=[1,31]

        

        //DISPLAY FORMATED DATE.

        SimpleDateFormat   formatter    = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss.SSS");

        String             formatedDate = formatter.format(new Date());

        System.out.println(formatedDate);             //26:03:2009 10:53:40.030

        

        //DISPLAY.

        System.out.println(text);                     //Tue Feb 08 17:04:07 CET 2005

        System.out.println(dayOfWeek        );        //0-Sunday,...,6-Suterday  Day of the week  [0, 6]

        System.out.println(dayOfMonth       );        //0-Sunday,...,6-Suterday  Day of the month [1,31]

        System.out.println(month            );        //[0,11]

        System.out.println(year        +1900);        //2012

        System.out.println(hours            );        //[0,23]

        System.out.println(monutes          );        //34

        System.out.println(seconds          );        //12

        System.out.println(yearInMiliseconds);        //1107879156077 urrent time in miliseconds.

        

        System.out.println(date1.getTime());          //1107879156077

        System.out.println(date2.getTime());          //1107879156077

        System.out.println(date3);                    //Sat Jan 12 00:00:00 CET 2005

        System.out.println(date4);                    //Sat Mar 12 12:59:01 CET 2005   

   

      }

   

    }