import java.util.Calendarimport java.util.Dateimport java.util.Scanner/** * */ /** * @author think * */ public class Test1 { /** * */ public Test1() { /帆兆/ TODO Auto-generated constructor stub } /** * function: * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Test1().print()} /** * function: 打印函数 包含对当前输入的月历的打印 以及输入和输出的打印 */ public void print(){ //当月
天数 //输入你的查询年月 如果直态渣租接输入月 默认是当前年 Scanner scanner = new Scanner(System.in) System.out.println("1.please input the month:")int month = scanner.nextInt() while(month <= 0 || month >12) { System.out.println("1.please input the month:")month = scanner.nextInt()}int days = days(month) String[] weeks = {"sun","mon","tue","wed","thr","Frd","sat"} for (int i = 0i <weeks.lengthi++) { System.out.print(weeks[i]) for (int j = 0j <2j++) { System.out.print(" ")} } System.out.println() int weekOfday = getWeekOfday(month,1)//获得第一天的
星期 for (int i = 0i <(weekOfday - 1) * 5i++) { System.out.print(" ")} for (int i = 1i <= daysi++,weekOfday +=1) { if(weekOfday == 8) { weekOfday = 1 System.out.println()System.out.print(i) } else { System.out.print(i)} for (int j = 0j <5 - (String.valueOf(i).length())j++) { System.out.print("梁散 ")} } System.out.println("\n") System.out.println("2.please input the date(format is like:20030204):")String date = scanner.next() while(date.length() <8) { System.out.println("2.please input the date(format is like:20030204):")date = scanner.next()} System.out.println("your input date weekday is:" + (getWeekDayStr(getWeekOfday(date)))) System.out.println("\n") System.out.println("3.please input the compute date1(format is like:20030204):")String date1 = scanner.next() while(date.length() <8) { System.out.println("3.please input the compute date1(format is like:20030204):")date1 = scanner.next()} System.out.println("3.please input the compute date2(format is like:20030204):")String date2 = scanner.next() while(date.length() <8) { System.out.println("3.please input the compute date2(format is like:20030204):")date2 = scanner.next()} int xiangchadays = daysxiangcha(date1, date2) System.out.println("xiangcha days is:" + xiangchadays)} /** * function: 通过准确的年月日查询星期几的
整数表示 * @param year * @param month * @param day * @return */ public int getWeekOfday(int year,int month, int day) { Calendar c = Calendar.getInstance() c.set(Calendar.YEAR, year) c.set(Calendar.MONTH, month - 1) c.set(Calendar.DAY_OF_MONTH, day) return c.get(Calendar.DAY_OF_WEEK)} /** * function: 通过输入月日 查询星期几的整数表示 默认是当前年 * @param month * @param day * @return */ public int getWeekOfday(int month, int day) { Calendar c = Calendar.getInstance() c.setTime(new Date()) int year = c.get(Calendar.YEAR) return getWeekOfday(year, month, day)} /** * function: 通过日期字符串获得星期几的整数表示 * @param dateStr * @return */ public int getWeekOfday(String dateStr) { int year =Integer.parseInt(dateStr.substring(0, 4)) int month =Integer.parseInt(dateStr.substring(4, 6)) int day =Integer.parseInt(dateStr.substring(6, 8)) return getWeekOfday(year, month, day)} /** * function: 返回星期几这样的表示 * @param weekday * @return */ public String getWeekDayStr(int weekday) { return weekDayStrs[weekday - Calendar.SUNDAY]} String[] weekDayStrs = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"} /** * function: 两个日期的相差天数 * @param dateStr1 20100205这样的格式 * @param dateStr2 20100205这样的格式 * @return */ public int daysxiangcha(String dateStr1,String dateStr2) { int year1 =Integer.parseInt(dateStr1.substring(0, 4)) int month1 =Integer.parseInt(dateStr1.substring(4, 6)) int day1 =Integer.parseInt(dateStr1.substring(6, 8)) int year2 =Integer.parseInt(dateStr2.substring(0, 4)) int month2 =Integer.parseInt(dateStr2.substring(4, 6)) int day2 =Integer.parseInt(dateStr2.substring(6, 8)) Calendar c1 = Calendar.getInstance() c1.set(Calendar.YEAR, year1) c1.set(Calendar.MONTH, month1 -1) c1.set(Calendar.DAY_OF_MONTH, day1) Calendar c2 = Calendar.getInstance() c2.set(Calendar.YEAR, year2) c2.set(Calendar.MONTH, month2 - 1) c2.set(Calendar.DAY_OF_MONTH, day2) long mills = c1.getTimeInMillis() >c2.getTimeInMillis()? c1.getTimeInMillis() - c2.getTimeInMillis(): c2.getTimeInMillis() - c1.getTimeInMillis() return (int)(mills/1000/3600/24)} /** * @param year * @param month * @return 某年某月的天数 */ public static int days(int year,int month) { int days = 0if(month!=2) { switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:days = 31 breakcase 4: case 6: case 9: case 11:days = 30} } else { if(year%4==0 &&year%100!=0 || year%400==0) days = 29else days = 28} return days} /** * function: 查询某月的天数 默认是当前年份 * @param month * @return */ public static int days(int month) { Calendar c = Calendar.getInstance() c.setTime(new Date()) int year = c.get(Calendar.YEAR) return days(year, month)} }import java.util.Calendar
import java.util.GregorianCalendar
public class MyDate {
public static void main(String[] args) {
Calendar date = Calendar.getInstance()
System.out.println(date.getTime())
System.out.println("者旁....................................")
System.out.println("当前年份:" + date.get(Calendar.YEAR) + " 年")
System.out.println("当前月份:" + (date.get(Calendar.MONTH) + 1) + " 月")
System.out.println("当前日期:" + date.get(Calendar.DATE) + " 日")
System.out.println("....................................")
System.out.println("小时:" + date.get(Calendar.HOUR) + " " + (date.get(Calendar.AM_PM) == 0 ? "PM" : "AM"))
System.out.println("小时:" + date.get(Calendar.HOUR_OF_DAY))
System.out.println("分钟:" + date.get(Calendar.MINUTE))
System.out.println("秒:" + date.get(Calendar.SECOND))
System.out.println("毫秒:" + date.get(Calendar.MILLISECOND))
System.out.println("....................................")
System.out.println(getWeekStr(date.get(Calendar.DAY_OF_WEEK)))
System.out.println("当前月份的第 " + date.get(Calendar.DAY_OF_MONTH) + " 天")
System.out.println("当前年份的第 " + date.get(Calendar.DAY_OF_YEAR) + " 天"迅嫌银)
System.out.println("....................................")
boolean b = ((GregorianCalendar) date).isLeapYear(date.get(Calendar.YEAR))
if (b) {
System.out.println("闰年")
} else {
System.out.println("平年"亩宴)
}
}
// 判断是否是闰年,是返回true,否则返回false
public static boolean isLeanYear(int year) {
boolean falg = false
if ((year % 4 == 0) &&(year % 100 != 0) || year % 400 == 0) {
falg = true
}
return falg
}
public static String getWeekStr(int num) {
switch (num) {
case 1:
return "星期 日"
case 2:
return "星期一 "
case 3:
return "星期 二 "
case 4:
return "星期三 "
case 5:
return "星期 四"
case 6:
return "星期 五 "
case 7:
return "星期六 "
}
return ""
}
}
评论列表(0条)