Wednesday, August 31, 2011

Lesson5 Date and Time in Java

In this lesson we will look at the features of using Date and Time in Java. There are three main classes for working with it:
-Date (a class normally used to represent dates and times)
-Calendar (a class normally used to manipulate dates)
-GregorianCalendar (a concrete subclass of Calendar that provides the standard calendar system)
   


 
// Converting  String to Date and reverse
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            date = sdf.parse("2011/08/29");
            System.out.println(date);
            System.out.println(sdf.format(date));


 
// Converting String to Date
            int year = 2011;
            int month = 8;
            int day = 29;
            String dateString = year + ":" + month + ":" + day;
            sdf = new SimpleDateFormat("yyyy:MM:dd");
            System.out.println(sdf.parse(dateString));



 //  result    241 29 2 5
            Date today = new Date();
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(date);
            System.out.println(cal.get(Calendar.DAY_OF_YEAR) + " " + cal.get(Calendar.DAY_OF_MONTH) + " " +
             cal.get(Calendar.
DAY_OF_WEEK) + " " + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
           



 //  result  Current date : 8-31-2011
            Calendar now = Calendar.getInstance();
            System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-" +
             now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));








 //  result  Current month is : Aug
            String[] strMonths = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            System.out.println("Current month is : " + strMonths[now.get(Calendar.MONTH)]);







// Getting Date from Calendar
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DATE, day);
            date = calendar.getTime();
            System.out.println(date);







 // Manipulating by Date
            GregorianCalendar todayDate = new GregorianCalendar();
            GregorianCalendar thisDate = new GregorianCalendar();
            thisDate.set(Calendar.YEAR, 2010);
            if (thisDate.before(todayDate)) {
                  System.out.println("before");
            }
            if (todayDate.after(thisDate)) {
                  System.out.println("after");
            }







 //Getting days name
            String[] weekdays = new DateFormatSymbols().getShortWeekdays();
            for (String s : weekdays) {
                  System.out.println(s);
            }







 // Date  Formatting
            DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT);
            DateFormat mediumDf = DateFormat.getDateInstance(DateFormat.MEDIUM);
            DateFormat longDf = DateFormat.getDateInstance(DateFormat.LONG);
            DateFormat fullDf = DateFormat.getDateInstance(DateFormat.FULL);
            System.out.println(shortDf.format(new Date()));// 8/31/11
            System.out.println(mediumDf.format(new Date()));// Aug 31, 2011
            System.out.println(longDf.format(new Date()));// August 31, 2011
            System.out.println(fullDf.format(new Date()));// Wednesday, August 31 2011







 // Manipulating by Calendar
            calendar = Calendar.getInstance();
            System.out.println("Original = " + calendar.getTime());
            calendar.add(Calendar.HOUR, -2);
            calendar.add(Calendar.MINUTE, 30);
            calendar.add(Calendar.SECOND, 300);
            System.out.println("Updated  = " + calendar.getTime());

      }

No comments:

Post a Comment