其他对象—— Date 和 Calendar 获取修改时间日期

来源:互联网 发布:女生做淘宝美工累吗 编辑:程序博客网 时间:2024/06/06 00:55
import java.util.*;import java.text.*;class DateDemo {public static void main(String[] args) {Date d = new Date();System.out.println(d);//打印的时间看不懂,希望有些格式。//将模式封装到SimpleDateformat对象中。SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日E hh:mm:ss");//调用format方法让模式格式化指定Date对象。String time = sdf.format(d);System.out.println("time="+time);long l = System.currentTimeMillis();Date d1 = new Date(l);System.out.println("d1:"+d1);}}
import java.util.*;import java.text.*;class  CalendarDemo{public static void main(String[] args) {Calendar c = Calendar.getInstance();String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};int index = c.get(Calendar.MONTH);int index1 = c.get(Calendar.DAY_OF_WEEK);sop(c.get(Calendar.YEAR)+"年");//sop((c.get(Calendar.MONTH)+1)+"月");sop(mons[index]);sop(c.get(Calendar.DAY_OF_MONTH)+"日");//sop("星期"+c.get(Calendar.DAY_OF_WEEK));sop(weeks[index1]);/*Date d = new Date();SimpleDateFormat sdf = new SimpleDateFormat("yyyy");String year = sdf.format(d);System.out.println(year);*/}public static void sop(Object obj){System.out.println(obj);}}
import java.util.*;/*两个练习:1,获取任意年的二月有多少天。思路:根据指定年设置一个时间就是 c.set(year,2,1)//某一年的3月1日。c.add(Calenar.DAY_OF_MONTH,-1);//3月1日,往前推一天,就是2月最后一天。2,获取昨天的现在这个时刻。c.add(Calenar.DAY_OF_MONTH,-1);*/class  CalendarDemo2{public static void main(String[] args) {Calendar c = Calendar.getInstance();//c.set(2012,2,23);c.add(Calendar.DAY_OF_MONTH,-18);printCalendar(c);}public static void printCalendar(Calendar c){String[] mons = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};String[] weeks = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六",};int index = c.get(Calendar.MONTH);int index1 = c.get(Calendar.DAY_OF_WEEK);sop(c.get(Calendar.YEAR)+"年");//sop((c.get(Calendar.MONTH)+1)+"月");sop(mons[index]);sop(c.get(Calendar.DAY_OF_MONTH)+"日");//sop("星期"+c.get(Calendar.DAY_OF_WEEK));sop(weeks[index1]);}public static void sop(Object obj){System.out.println(obj);}}




阅读全文
0 0
原创粉丝点击