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();
}
}
I just signed up to your blogs rss feed. Will you post more on this subject?
ReplyDeleteWhat a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.
ReplyDelete