`
JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com





Date And Time

  • Major classes
    • java.util.Date
      used to represent a single point of date/time
    • java.util.Calendar
      used for all kinds of operations on date/time

Creating an object to represent the current date and time


Date now=new Date();
Or
Calendar cal=Calendar.getInstance();
Date now=cal.getTime();

Date/Time Components

  • What year is it? What day is it? What week is it?
  • Use the get() method of the calendar to get various part of a date/time
    • cal.get(Calendar.YEAR)
    • cal.get(Calendar.DAY_OF_WEEK)

Comparing Date/Time

  • How to know which date is later, or earlier?
    • Is the book due today?
    • Who is older?
    • Did I pay rent on time?
  • “after()”, “before()”, equals()
Formatting Date and Time

  • java.text.SimpleDateFormat
    • Use pattern to customize the display
Date date1=new Date();
SimpleDateFormat sdf=new SimpleDateFormat();
sdf.applyPattern(pattern);
String formattedDate1=sdf.format(date1);

Example:-1
import java.util.Calendar;
import java.util.Date;

public class date1 {

  public static void main(String[] args) {
    Date d1 = new Date();

    System.out.println(d1);

    System.out.println(d1.getHours() ":" + d1.getMinutes());

    Calendar cal = Calendar.getInstance();
    System.out.println(cal.get(Calendar.HOUR":"
        + cal.get(Calendar.MINUTE));
  }
}
Output:=

java date1
Tue Sep 13 13:16:25 PDT 2011
13:16
1:16
Donwload code

Example: 2


import java.util.Calendar;
import java.util.Date;

public class date2 {
  public static void main(String[] args) {
    long l = 1000 60 60 24 3650L;
    Date d1 = new Date(l);

    System.out.println(d1);

    Calendar cal = Calendar.getInstance();

    cal.clear();

    cal.set(201201);
    
    System.out.println(cal.getTime());
    
     cal.add(Calendar.MONTH, 6);
     System.out.println(cal.getTime());
     
     cal.add(Calendar.HOUR, 2);
     cal.add(Calendar.MINUTE, 30);
     
     System.out.println(cal.getTime());
     
      long diff=cal.getTimeInMillis()-
 Calendar.getInstance
().getTimeInMillis();
     
     System.out.println("diff in days is "
+
(diff/(24*1000*60*60L)));
     System.out.println(cal.after(Calendar.
getInstance
()));
  }
}
Output:-

java date2
Sat Dec 29 16:00:00 PST 1979
Sun Jan 01 00:00:00 PST 2012
Sun Jul 01 00:00:00 PDT 2012
Sun Jul 01 02:30:00 PDT 2012
diff in days is 291
true
Downlaod ocde

Example 3:-
import java.util.Calendar;
import java.util.Date;

public class calender1 {

  public static void main(String[] args) {
    Date d1 = new Date();
    long l1 = d1.getTime();
    System.out.println(new Date(l1));

    System.out.println(d1.getHours() ":" + d1.getMinutes());

    Calendar cal = Calendar.getInstance();

    System.out.println("calender time is " + cal.getTime());

    System.out.println("year is " + cal.get(Calendar.YEAR));
int x=cal.get(Calendar.AM_PM);
String h;
if(x==0)
  h="am";
else
  h="PM";
    String t1 = cal.get(Calendar.HOUR" : " +
cal.get
(Calendar.MINUTE)+" "+h;
    System.out.println(t1);
System.out.println(x);
  }
}
Output

java calender1
Tue Sep 13 13:25:28 PDT 2011
13:25
calender time is Tue Sep 13 13:25:28 PDT 2011
year is 2011
1 : 25 PM
1
Download code

Example: 4

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class calender2 {

  public static void main(String[] args) {
    long l = 1000 60 60 24 3650L;
    Date d1 = new Date(l);

    System.out.println(d1);

    Calendar cal = Calendar.getInstance();

    cal.clear();

    cal.set(201201);

    // System.out.println(cal.getTime());
    // cal.add(Calendar.MONTH, 6);
    // System.out.println(cal.getTime());
    // cal.add(Calendar.HOUR, 2);
    // cal.add(Calendar.MINUTE, 30);
    //
    System.out.println(cal.getTime());

    long l1 = cal.getTimeInMillis()
        - Calendar.getInstance().getTimeInMillis();

    System.out.println(l1 / (1000 60 60 24L));

    System.out.println(cal.after(Calendar.getInstance()));
    Date d2 = Calendar.getInstance().getTime();

    SimpleDateFormat sdf = new SimpleDateFormat();

    sdf.applyPattern("MM/dd/yy");
    String s = sdf.format(d2);
    System.out.println(s);
    String s1="10/07/2011";
    try{
    Date d3=sdf.parse(s1);
    System.out.println(d3);
    }
    catch (Exception e) {
       System.err.println(e);
    }
  }

}
Output;

java calender2
Sat Dec 29 16:00:00 PST 1979
Sun Jan 01 00:00:00 PST 2012
109
true
09/13/11
Fri Oct 07 00:00:00 PDT 2011
lDownload code

Example 5:

import java.text.SimpleDateFormat;
import java.util.Date;

public class dateformat1 {

  public static void main(String[] args) {
    Date d1 = new Date();
    System.out.println(d1);

    SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yy");
    
    System.out.println(sf.format(d1));
    
    SimpleDateFormat sf1 = new SimpleDateFormat();
    sf1.applyPattern("yyyy-MM-dd");
    
    System.out.println(sf1.format(d1));
    sf1.applyPattern("hh:mm");
    System.out.println(sf1.format(d1));

  }

}
Output

java dateformat1
Tue Sep 13 13:30:20 PDT 2011
13/09/11
2011-09-13
01:30
Download code