您的位置:首页 > 其它

原 自学JVAVA---(30)--(内功心法【28】)日期时间类

2019-06-01 17:50 58 查看

日期时间
【小城贝尔】
匆匆光阴流逝间,Date毫 秒 来 展 现。
时间展示格式化,计算参考calendar。

public static void main(String[] args) throws Exception{
Date date = new Date();
System.out.println(date.getTime());

SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str =  sd.format(date);
System.out.println(str);

SimpleDateFormat sd1 =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sd1.parse("1997-06-14 06:32:33");
System.out.println(date1.toString());

Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.YEAR ,1997);
calendar.set(Calendar.MONTH , 06);
calendar.set(Calendar.DAY_OF_MONTH,14);
System.out.println(sd.format(calendar.getTime()));
int year = calendar.get(Calendar.YEAR);
int month =  calendar.get(Calendar.MONTH);
System.out.println("YEAR :  "+year +"   MONTH : "+month);

}

打印日历

/**
* printCalendar("2016-02-10");
* 周日	周一	周二	周三	周四	周五	周六
* 		1		2		3		4		5		6
* 7	8		9		10*		11		12		13
* 14	15		16		17		18		19		20
* 21	22		23		24		25		26		27
* 28	29
*
*/
public static void printCalendar(String date) throws Exception{
SimpleDateFormat sd1 =  new SimpleDateFormat("yyyy-MM-dd");
Date parseDate = sd1.parse(date);
Calendar calen = new GregorianCalendar();
calen.setTime(parseDate);
//获得你输入的天
int today = calen.get(Calendar.DAY_OF_MONTH);
//获得这个月有多少天
int days = calen.getActualMaximum(Calendar.DAY_OF_MONTH);
//获得本月一号是周几
calen.set(Calendar.DAY_OF_MONTH ,1);
int week = calen.get(Calendar.DAY_OF_WEEK);
System.out.println("周日\t周一\t周二\t周三\t周四\t周五\t周六");
for(int i = 1 ; i < week ; i++){
System.out.print("\t\t");
}
for(int i = 1; i <= days ; i ++){
if(i == today){
System.out.print(i +"*\t\t");
}else {
System.out.print(i+"\t\t");
}
//将作为日期设置给当前calen便于确定周几然后换行
calen.set(Calendar.DAY_OF_MONTH,i);
if(calen.get(Calendar.DAY_OF_WEEK) == 7){
System.out.println();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: