首先上传转换时间的工具代码类DateUtil.java ,日历比较时间的时候会用到
package com.JavaStudy.study;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public DateUtil() {
super();
// TODO Auto-generated constructor
}
public static final String DEFAULT_DATE_SAMPLE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_TIME_SAMPLE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final java.text.SimpleDateFormat DATE_FORMATTER = new java.text.SimpleDateFormat(
DEFAULT_DATE_SAMPLE_FORMAT);
private static final java.text.SimpleDateFormat TIME_FORMATTER = new java.text.SimpleDateFormat(
DEFAULT_TIME_SAMPLE_FORMAT);
//转换日期到字符串 带十 分 秒
public static String getTimeSampleString(java.util.Date date) {
if (date == null)
return null;
return TIME_FORMATTER.format(date);
}
//转换日期到字符串 不带十 分 秒
public static String getDateSampleString(java.util.Date date) {
if (date == null)
return null;
return DATE_FORMATTER.format(date);
}
//按照指定格式化日期 返回日期字符串
public static String getDateSampleString(java.util.Date date, String format) {
if (date == null)
return null;
if (format == null)
return getDateSampleString(date);
java.text.SimpleDateFormat dateFormater = new java.text.SimpleDateFormat(
format);
return dateFormater.format(date);
}
//得到当前年份
public static int getCurYear() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
return year;
}
//得到当前月份
public static int getCurMonth() {
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH) + 1;
return month;
}
//得到当前是几号
public static int getCurDay() {
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
return day;
}
//得到当前小时
public static int getCurHour() {
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR);
return hour;
}
//得到当前分
public static int getCurMinute() {
Calendar cal = Calendar.getInstance();
int minute = cal.get(Calendar.MINUTE);
return minute;
}
//得到当前秒
public static int getCurSecond() {
Calendar cal = Calendar.getInstance();
int second = cal.get(Calendar.SECOND);
return second;
}
//转换日期字符串到Timestamp类型
public static Timestamp getTimestamp(String dateTimeFormat) {
String year = null, month = null, date = null, hour = null, minute = null, second = null;
if (dateTimeFormat == null)
return null;
if (dateTimeFormat.length() > 3)
year = dateTimeFormat.substring(0, 4);
if (dateTimeFormat.length() > 6)
month = dateTimeFormat.substring(5, 7);
if (dateTimeFormat.length() > 9)
date = dateTimeFormat.substring(8, 10);
if (dateTimeFormat.length() > 12)
hour = dateTimeFormat.substring(11, 13);
if (dateTimeFormat.length() > 15)
minute = dateTimeFormat.substring(14, 16);
if (dateTimeFormat.length() > 18)
second = dateTimeFormat.substring(17);
StringBuffer str = new StringBuffer();
if (year != null)
str.append(year);
if (month != null) {
str.append("-");
str.append(month);
}
if (date != null) {
str.append("-");
str.append(date);
}
if (hour != null) {
str.append(" ");
str.append(hour);
}
if (minute != null) {
str.append(":");
str.append(minute);
}
if (second != null) {
str.append(":");
str.append(second);
}
if (year != null)
return Timestamp.valueOf(str.toString());
else
return null;
}
//转换日期字符串为Date类型 带 时分秒
public static Date getDate(String time) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.util.Date date = null;
try {
date = bartDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
//转换日期字符串为Date类型 不带 时分秒
public static Date getBirthDate(String time) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = null;
try {
date = bartDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
// 获取当前系统时间
public static Date getCurrentTime() {
Date dateTime = new Date();
SimpleDateFormat bartDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String todayAsString = bartDateFormat.format(dateTime);
java.util.Date date = null;
try {
date = bartDateFormat.parse(todayAsString);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static void main(String[] args) {
int year = DateUtil.getCurYear();
int month = DateUtil.getCurMonth();
int day = DateUtil.getCurDay();
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(DateUtil.getCurHour());
System.out.println(DateUtil.getCurMinute());
System.out.println(DateUtil.getCurSecond());
Date date1 = new Date();
System.out.println(date1.toString());
System.out.println(DateUtil.getTimeSampleString(date1));
System.out.println(DateUtil.getDateSampleString(date1));
Date date2 = DateUtil.getDate("2011-11-8 12:26:21");
System.out.println(date2.toString());
System.out.println(DateUtil.getTimeSampleString(date2));
}
}
万年历类calendar.java:
package com.JavaStudy.study;
import java.util.Scanner;
public class calendar {
Scanner sc = new Scanner(System.in);
/**
* 输入年和月,显示当月的日历
1、输入年和月
2、计算月的天数
3、1900年1月1日是星期一
计算出月的1号距离1900年1月1日的天数
4、循环输出日历
5、当天需要选中**/
public static void main(String[] args) {
System.out.print("****************欢迎使用万年历*************\n");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
System.out.print("请输入月份:");
int month = scanner.nextInt();
// 1900年1月1日是星期一
// 7天后 1月8日 星期1 1 = 7%7+1
// 15天后 1月16日星期2 2 = 15%7+1
// 20天后 1月21号星期 7 = 20%7+1
// 首先计算当前年份距离1900年的天数
// 年份 能被4整除但不能被100整除 或者 能被400整除的 就是闰年
int totalDays = 0;
for (int i = 1900; i < year; i++) {
if ((i % 4 == 0 && !(i % 100 == 0)) || i % 400 == 0)
totalDays = totalDays + 366;
else
totalDays = totalDays + 365;
}
// 计算当年到当月的天数
// 判断当年是闰年还是平年
boolean isRn = false;
if ((year % 4 == 0 && !(year % 100 == 0)) || year % 400 == 0) {
isRn = true;
} else {
isRn = false;
}
// 计算当年到当月的天数 1 3 5 7 8 10 12
// 并记录当月的天数
int days = 0;
for (int i = 1; i <= month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
if (isRn)
days = 29;
else
days = 28;
break;
default:
days = 30;
}
if (i < month)
totalDays += days;
}
// 计算当月的第一天是星期几
int weekday = 1;
weekday = totalDays% 7 + 1;
System.out.print("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
for (int i = 0; i < weekday; i++) {
System.out.print("\t");
}
for (int i = 1; i <= days; i++) {
if (DateUtil.getCurYear()==year && DateUtil.getCurMonth()==month && DateUtil.getCurDay()==i){
System.out.print("["+i+"]" + "\t");
}else
System.out.print(i+ "\t");
// 如果当天为周六 输出换行
if ((totalDays + i ) % 7 == 6)
System.out.print("\n");
}
}
}
最终的运行效果为: