今天依然记录一下,学习情况,可以看到自己每一天都在进步。
1 import java.text.DateFormat; 2 import java.text.ParseException; 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 /** 7 * 测试时间对象和字符串之间的相互转换 8 * DateFormat抽象类和SimpleDateFormat实现类 9 * @author 小白 10 * 11 */ 12 13 public class TestDateFormat { 14 public static void main(String[] args) throws ParseException { 15 16 //把时间对象按照“格式字符串指定的格式”转成相应的字符串 17 DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss"); 18 String str = df.format(new Date(40000000)); 19 System.out.println(str); 20 21 //把字符串按照“格式字符串指定的格式”转成相应的时间对象 22 DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 23 Date date = df2.parse("1983-05-10 10:44:34"); 24 System.out.println(date); 25 26 //测试其他的格式字符。比如:利用D,获得本时间对象是所处年份的第几天 27 DateFormat df3 = new SimpleDateFormat("D"); 28 String str3 = df3.format(new Date()); 29 System.out.println(str3); 30 31 32 } 33 34 }
1 import java.util.Calendar; 2 import java.util.Date; 3 import java.util.GregorianCalendar; 4 5 /** 6 * 测试日期类的使用 7 * @author 小白 8 * 9 */ 10 11 public class Testcalender { 12 public static void main(String[] args) { 13 14 Calendar calendar = new GregorianCalendar(2999, 10, 9, 22, 10, 50); 15 int year = calendar.get(Calendar.YEAR); 16 int month = calendar.get(Calendar.MONTH); 17 int day = calendar.get(Calendar.DATE); 18 int weekday = calendar.get(Calendar.DAY_OF_WEEK);//星期几。1-7 19 20 System.out.println(year); 21 System.out.println(day); 22 System.out.println(month);//0-11表示对应的月份,0是1月,1是2月,依次类推 23 System.out.println(weekday); 24 25 26 27 //设置日期的相关元素 28 Calendar c2 = new GregorianCalendar(); 29 c2.set(Calendar.YEAR, 8012); 30 31 System.out.println(c2); 32 33 //日期的计算 34 Calendar c3 = new GregorianCalendar(); 35 c3.add(Calendar.DATE, 100); 36 System.out.println(c3); 37 38 //日期对象和时间对象的转化 39 Date d4 = c3.getTime(); 40 Calendar c4 = new GregorianCalendar(); 41 c4.setTime(new Date()); 42 43 printCalendar(c4); 44 } 45 46 public static void printCalendar(Calendar c){ 47 //打印:1918年10月10日 11:23:45 周三 48 49 int year = c.get(Calendar.YEAR); 50 int month = c.get(Calendar.MONTH)+1; 51 int date = c.get(Calendar.DAY_OF_MONTH); 52 int dayweek = c.get(Calendar.DAY_OF_WEEK); 53 54 String dayweek2 = dayweek==0?"日":dayweek+""; 55 int hour = c.get(Calendar.HOUR); 56 int minte = c.get(Calendar.MINUTE); 57 int second = c.get(Calendar.SECOND); 58 59 System.out.println(year+"年"+month+"月"+date+"日"+hour+"时"+minte+"分"+second+"秒"+ 60 "周"+dayweek2); 61 62 } 63 }
1 import java.text.DateFormat; 2 import java.text.ParseException; 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6 import java.util.GregorianCalendar; 7 import java.util.Scanner; 8 9 /** 10 * 可视化日历程序 11 * @author 小白 12 * 13 */ 14 public class TestCalendar02 { 15 public static void main(String[] args) throws ParseException { 16 System.out.println("请输入日期(如:2010-10-20):"); 17 Scanner scanner = new Scanner(System.in); 18 String str = scanner.nextLine(); 19 20 DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 21 Date date = df.parse(str);//时间对象 22 Calendar c = new GregorianCalendar(); 23 c.setTime(date); 24 int day = c.get(Calendar.DAY_OF_MONTH); 25 int days = c.getActualMaximum(Calendar.DAY_OF_MONTH); 26 27 System.out.println("日 一 二 三 四 五 六 "); 28 29 c.set(Calendar.DAY_OF_MONTH, 1); 30 31 for(int i=0;i<c.get(Calendar.DAY_OF_WEEK)-1;i++){ 32 System.out.print(" "); 33 } 34 35 for(int i=0;i<days;i++){ 36 37 if(c.get(Calendar.DAY_OF_MONTH)==day){ 38 System.out.print(c.get(Calendar.DAY_OF_MONTH)+"* "); 39 }else{ 40 System.out.print(c.get(Calendar.DAY_OF_MONTH)+" "); 41 } 42 43 if(c.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){ 44 System.out.println();//换行 45 } 46 47 c.add(Calendar.DAY_OF_MONTH, 1); 48 } 49 50 51 } 52 }