java的日期

Posted 不想下火车的人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java的日期相关的知识,希望对你有一定的参考价值。

  直接看例子:

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的日期的主要内容,如果未能解决你的问题,请参考以下文章

JS代码片段:一个日期离现在多久了

sql 日期转换代码片段 - Dato,120,konvertere

java字符串格式转换成日期格式

日期选择器对话框在片段中不起作用[关闭]

如何从片段中调用 getSupportFragmentManager()?

vscode 用户代码片段 vue初始化模板 Snippet #新加入开头注释 自动生成文件名 开发日期时间等内容