随笔记载-时间和日历

随笔记载-时间和日历

Java中的Date类中的获取年、月getYear(),getMonth()不推荐使用,

代码块

代码块语法遵循标准markdown代码,例如:

package com.ecode.li.date;  
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectOutputStream; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.GregorianCalendar;  
public class CalendarTest {  /** 
  * Calendar类使用详解   *    * @param   */ 
 public static void main(String[] args) {   // 实例化 
  Calendar cale = Calendar.getInstance();   
  if (cale instanceof GregorianCalendar) {    System.out.println("这说明产生了一个GregorianCalendar类对象,且该类是Calendar抽象类的子类
");   } 
  // 从一个 Calendar 对象中获取 Date 对象 
    Date date = cale.getTime();   /* 
   * 将 Date 对象反应到一个 Calendar 对象中, Calendar/GregorianCalendar 没有构造函数可以接受    * Date 对象 所以我们必需先获得一个实例,然后设置 Date 对象    */ 
  cale.setTime(date);   // 当前年 
  int year = cale.get(Calendar.YEAR);   System.out.println(year);  
  // 当前月 Calendar.MONTH从0开始 
  int month = (cale.get(Calendar.MONTH)) + 1;   System.out.println(month);  
  // 当前月的第几天:即当前日   
  int day_of_month = 
cale.get(Calendar.DAY_OF_MONTH); 
 // Calendar.DAY_OF_MONTHCalendar.DATE 是等价的 
      int dates = cale.get(Calendar.DATE);
      System.out.println(dates); 
      System.out.println(day_of_month);  
  // 当前时:HOUR_OF_DAY-24小时制 
  int hour24 = cale.get(Calendar.HOUR_OF_DAY);   // HOUR-12小时制 
  int hour12 = cale.get(Calendar.HOUR);   System.out.println(hour24);   System.out.println(hour12);  
  // 当前分 
  int minute = cale.get(Calendar.MINUTE);   // 当前秒 
  int second = cale.get(Calendar.SECOND);   System.out.println(minute);   System.out.println(second);  
  // 星期几 Calendar.DAY_OF_WEEK用数字(1~7)表示(星期日~星期六) 

  int day_of_week = cale.get(Calendar.DAY_OF_WEEK) - 1;   System.out.println(day_of_week);  
  // 当前年的第几周   
  int week_of_year = 
cale.get(Calendar.WEEK_OF_YEAR);   System.out.println(week_of_year);  
  // //当前月的星期数   // 
  int week_of_month = cale.get(Calendar.WEEK_OF_MONTH);   //      
  // //当前月中的第几个星期 
  // int day_of_week_in_month = cale.get(Calendar.DAY_OF_WEEK_IN_MONTH);   //      
  // //当前年的第几天   // int day_of_year = cale.get(Calendar.DAY_OF_YEAR);  } } 

本月日历

代码块:
python
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Test2 {
public static void main(String[] args) {

    //日历类
    GregorianCalendar d = new GregorianCalendar();
    //当天
    int today = d.get(Calendar.DAY_OF_MONTH);
    //当月
    int month = d.get(Calendar.MONTH);
    //设置当月第一天
    d.set(Calendar.DAY_OF_MONTH, 1);
    //获取第一天星期
    int weekday = d.get(Calendar.DAY_OF_WEEK);
    //每周第一天
    int firstDayOfWeek = d.getFirstDayOfWeek();
    //本月第一天是周几,前面空对应位置
    int indent = 0;
    while (weekday != firstDayOfWeek) {
        indent++;
        d.add(Calendar.DAY_OF_MONTH, -1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    }
    //星期几数组
    String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
    //遍历输出星期几
    do {System.out.printf("%4s",weekdayNames[weekday]);
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    } while (weekday != firstDayOfWeek);
    System.out.println();
    //本月第一天前面的空格
    for (int i = 0; i < indent; i++) {
        System.out.print("      ");
    }
    //输出每天
    d.set(Calendar.DAY_OF_MONTH, 1);
    do {
        int day = d.get(Calendar.DAY_OF_MONTH);
        System.out.printf("%5d",day);
        //判断是否是当天
        if (day == today) {
            System.out.print("*");
        }else{
            System.out.print(" ");
        }
        //日期加1
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
        //星期是否是第一天
        if (weekday == firstDayOfWeek) {
            System.out.println();
        }
        //是否超出本月
    } while (d.get(Calendar.MONTH) == month)
}

}

阅读更多

没有更多推荐了,返回首页