获取当前的时间,按照自定义的方法来输出
import java.util.Calendar;
public class ClenderDemos
{/**
*Calendar 类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR
*等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法*/
public static void main(String[] args)
{
//创建一个Calendar实现对象
Calendar c = Calendar.getInstance();
method1(c);
}
private static void method1(Calendar c) {
//
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
System.out.println(year + "年\t" + month + "月\t" + day + "日\t" + getWeek(week));
}
private static String getWeek(int week)
{
// TODO Auto-generated method stub
String[] weeks = { "","星期日" ,"星期一" ,"星期二" ,"星期三" ,"星期四" ,"星期五" ,"星期六" };
return weeks[week];
}
}
对其进行一些简单的设置
import java.util.Calendar;
public class ClenderDemos
{/**
*Calendar 类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR
*等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法*/
public static void main(String[] args)
{
//创建一个Calendar实现对象
Calendar c = Calendar.getInstance();
c.set(2015,6,17);
c.add(Calendar.YEAR,-2);
method1(c);//2013年 7月 17日 星期三
}
private static void method1(Calendar c) {
//
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
System.out.println(year + "年\t" + month + "月\t" + day + "日\t" + getWeek(week));
}
private static String getWeek(int week)
{
// TODO Auto-generated method stub
String[] weeks = { "","星期日" ,"星期一" ,"星期二" ,"星期三" ,"星期四" ,"星期五" ,"星期六" };
return weeks[week];
}
}
获取每年的二月有多少天
import java.util.Calendar;
public class ClenderDemos
{/**
*Calendar 类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR
*等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法*/
public static void main(String[] args)
{
//创建一个Calendar实现对象
Calendar c = Calendar.getInstance();
int year = 2016;
method2(c,year);//2016年 2月 29日 星期一
}
private static void method2(Calendar c,int year) {
// TODO Auto-generated method stub
c.set(year,2,1);
c.add(Calendar.DAY_OF_MONTH,-1);
int years = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
int week = c.get(Calendar.DAY_OF_WEEK);
System.out.println(years + "年\t" + month + "月\t" + day + "日\t" + getWeek(week));
}