Header Ads

  • Breaking Now

    Code Snippets:Experimenting With java.util.Date

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class ExperimentingWithDates {
    
    public static void daysInMonth() {
    Calendar c1 = Calendar.getInstance(); // new GregorianCalendar();
    c1.set(2096, 2, 20);
    int year = c1.get(Calendar.YEAR);
    int month = c1.get(Calendar.MONTH);
    int[] daysInMonths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    daysInMonths[1] += ExperimentingWithDates.isLeapYear(year) ? 1 : 0;
    String st = "";
    if (month == 1) {
    st = "st";
    } else if (month == 2) {
    st = "nd";
    } else if (month == 3) {
    st = "rd";
    } else {
    
    st = "th";
    }
    System.out.println("Days in " + month + st + " month for year " + year
    + " are " + daysInMonths[c1.get(Calendar.MONTH) - 1]);
    System.out.println();
    System.out.println();
    }
    
    public static boolean isLeapYear(int year) {
    boolean value = false;
    if (year % 100 == 0) {
    if (year % 400 == 0) {
    
    value = true;
    }
    } else if (year % 4 == 0) {
    value = true;
    } else {
    value = false;
    }
    return value;
    }
    
    public static void main(String args[]) {
    
    String DATE_FORMAT = "dd-MM-yyyy"; // Refer Java DOCS for formats
    DateFormat df = new SimpleDateFormat("EEEE");
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
    DATE_FORMAT);
    Calendar calendar = Calendar.getInstance();
    Calendar otherCalendar = Calendar.getInstance();
    Date date = new Date();
    String day = df.format(date);
    
    System.out.println("Today's date in Calendar Format : " + calendar);
    System.out.println("Today is " + day);
    System.out.println("calendar.getTime() : " + calendar.getTime());
    System.out.println("calendar.get(Calendar.YEAR): "
    + calendar.get(Calendar.YEAR));
    System.out.println("Today's date in Date Format : " + date);
    
    calendar.set(1996, 0, 15); // (year,month,date)
    otherCalendar.set(1996, 0, 16);
    System.out.println("######");
    System.out.print("Days Between " + calendar.getTime() + "\t"
    + otherCalendar.getTime() + " is  ");
    System.out.println((otherCalendar.getTime().getTime() - calendar
    .getTime().getTime())
    / (24 * 3600 * 1000));
    if (calendar.before(otherCalendar)) {
    
    System.out.println("Date " + sdf.format(calendar.getTime())
    + " is before " + sdf.format(otherCalendar.getTime()));
    } else if (calendar.after(otherCalendar)) {
    
    System.out.println("Date " + sdf.format(calendar.getTime())
    + " is after " + sdf.format(otherCalendar.getTime()));
    }
    
    System.out.println("######");
    System.out.println("calendar.set(1999,0 ,15) : " + calendar.getTime());
    // adding 20 days to Jan 20th 1999.
    calendar.add(Calendar.DATE, 20);
    System.out.println("Date + 20 days is : "
    + sdf.format(calendar.getTime()));
    
    // roll down, substracts 1 month
    calendar.roll(Calendar.MONTH, false);
    System.out.println("Date is rolled down by 1 month : "
    + sdf.format(calendar.getTime()));
    System.out.println("Reset the date back to Jan 15, 1996");
    calendar.set(1996, 0, 15);
    System.out.println("Date is : " + sdf.format(calendar.getTime()));
    calendar.add(Calendar.MONTH, -1);
    // substract 1 month
    System.out.println("Date minus 1 month : "
    + sdf.format(calendar.getTime()));
    System.out.println();
    System.out.println();
    
    System.out.println("######");
    
    //shows number of days in a month of a year
    daysInMonth();
    
    
    }
    
    }
    

    Post Top Ad

    Post Bottom Ad