本文实例为大家分享了java自定义日历的具体代码,供大家参考,具体内容如下
效果图:
源码:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
package com.example;
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Scanner;
public class Test12 {
public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); System.out.println( "请输入年份!查询范围是 1900-2900" ); int year = scanner.nextInt(); System.out.println( "请输入月份!查询范围是 1-12" ); int month = scanner.nextInt(); System.out.println( "请输入日期!查询范围是1-" + getMonth(month, year)); int day = scanner.nextInt();
Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1 ); calendar.set(Calendar.DAY_OF_MONTH, day);
System.out.println(year + "年" + (isRunYear(year) ? "是闰年" : "不是闰年" )); System.out.println( "本月有" + getMonth(month, year) + "天" ); int totalDays = 0 ; for ( int i = 1900 ; i < year; i++) { totalDays += isRunYear(i) ? 366 : 365 ; } int nowDays = calendar.get(Calendar.DAY_OF_YEAR); totalDays += nowDays; System.out.println(year + "-" + month + "-" + day + "距1900年1月1日已有" + totalDays + "天" ); System.out.println(year + "-" + month + "-" + day + "距本年1月1日已有" + nowDays + "天" ); calendar.set(Calendar.DAY_OF_MONTH, 1 ); System.out.println( "本月的第一天为" + getFormatTime( "EEEE" , calendar.getTime()));
System.out.println( "本月日历为:" );
// 这里只是简单的输出字符串,假如在 Android Studio 的类似的开发平台中自定义日历的话,可以将泛型 String 换 // 成相应的 JavaBean 对象来存储数据和做标记,如标记是当月,是否被选中等。
List<String> list = new ArrayList<>();
// 得到本月一号的星期索引 // 索引从 1 开始,第一个为星期日,减 1 是为了与星期对齐,如星期一对应索引1,星期二对应索引二 int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1 ; // 拿到上一个月的最后几天的天数 for ( int i = 0 ; i < weekIndex; i++) { list.add( "" ); }
int currentDays = getMonth(month, year);
// 拿到当月的天数 for ( int i = 0 ; i < currentDays; i++) { int days = i + 1 ; if (days == day) { list.add( "@" + days); } else { list.add(days + "" ); } }
// 拿到下个月第一周的天数 // 先拿到本月最后一天的星期索引 calendar.set(Calendar.DAY_OF_MONTH, currentDays); weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1 ;
for ( int i = 0 ; i < 6 - weekIndex; i++) { list.add( "" ); }
String[] weeks = { "星期日" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" }; for ( int i = 0 ; i < weeks.length; i++) { System.out.print(getFormatString(weeks[i])); }
System.out.println();
int i = 0 ; for (String s : list) { System.out.print(getFormatString(s)); if ((i + 1 ) % 7 == 0 ) { System.out.println(); } i++; } System.out.println( "谢谢使用!" ); }
public static String getFormatString(String s) { // return String.format("%8s", s); return s + "\t" ; } // 判断是否为闰年 public static boolean isRunYear( int y) { return y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ; }
public static String getFormatTime(String p, Date t) { return new SimpleDateFormat(p, Locale.CHINESE).format(t); } // 获取当月的天数 public static int getMonth( int m, int y) { switch (m) { case 2 : return isRunYear(y) ? 29 : 28 ; case 4 : case 6 : case 9 : case 11 : return 30 ; default : return 31 ; } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/weixin_44116706/article/details/93530270