码迷,mamicode.com
首页 > 编程语言 > 详细

java的日期

时间:2018-08-21 00:29:57      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:col   system   ati   today   for   输出   pre   std   ==   

  直接看例子:

import java.text.DateFormatSymbols;
import java.util.*;

import org.springframework.cglib.core.Local;



public class CalendarTest
{
    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(Locale.US).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 = 1; 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("%3d",day);
            
            if(day == today)
            {
                System.out.print("*");
            }
            else{
                System.out.print(" ");
            }
            
            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);
        
        if(weekday != firstDayOfWeek)
        {
            System.out.println();
        }
    }
}

  输出:

 Sun Mon Tue Wed Thu Fri Sat
              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  30  31 

 

java的日期

标签:col   system   ati   today   for   输出   pre   std   ==   

原文地址:https://www.cnblogs.com/wuxun1997/p/9508883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!