本文共 1697 字,大约阅读时间需要 5 分钟。
//日历类
1.日历类的获取package com.hero;import java.text.SimpleDateFormat;
import java.util.Calendar;import java.util.Date;public class CalendarDemo {
public staticvoid sop(T t){ System.out.println(t);}public static void main(String[] args) { Calendar c = Calendar.getInstance();//默认时区和语言环境获得一个日历 String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; String[] weks = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}; int index1 = c.get(Calendar.MONTH);//获得这个月,0代表1月 int index2 = c.get(Calendar.DAY_OF_WEEK);//获得这个星期的第几天 sop(c.get(Calendar.YEAR)+"年");//返回当前年 sop(c.get(Calendar.MONTH)+1+"月"); sop(mons[index1]); sop(c.get(Calendar.DAY_OF_MONTH));//返回这个月的天数 sop(c.get(Calendar.HOUR)+"时"); sop(c.get(Calendar.MINUTE)+"分"); sop(c.get(Calendar.SECOND)+"秒"); sop(weks[index2-2]); sop(c.get(Calendar.DAY_OF_WEEK)); sop(c.getTimeInMillis());//获得当前类的毫秒数 Date d = new Date();//格式化输出日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String year = sdf.format(d); System.out.println("今天的日期为:"+year);}
}
2.日历类的设置package com.hero;import java.util.Calendar;
public class CalendarDemo2 {
public staticvoid sop(T t) { System.out.println(t);}public static void printCalendar(Calendar c){ sop(c.get(Calendar.YEAR)+"年"); sop(c.get(Calendar.MONTH)+"月"); sop(c.get(Calendar.DAY_OF_MONTH)+"日"); sop(c.get(Calendar.DAY_OF_WEEK)-1);}public static void main(String[] args) { Calendar c = Calendar.getInstance(); c.set(2018,10,29); printCalendar(c); }
}
//学习自云栖社区:吞吞吐吐的转载地址:http://naqax.baihongyu.com/