首先 希望疫情 尽快结束 大家以后拒绝野味呀 !!!
直接上干货 代码
这里把 主函数上传上去了 因为声明异常了 所以不省略了
public static void main(String[] args) throws ParseException {//这里偷懒 直接异常声明
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要查询的日期:");
String dateStr =scanner.next();
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
Date data = sim.parse(dateStr);
Calendar calendar = new GregorianCalendar(); //实例化一个标准日历
calendar.setTime(data); //将时间赋值给calendar
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1); //用来赋值当前日期这个月的第几天
//DAY_OF_MONTH 当前日期 是这个月的第几天
//Calendar.DAY_OF_YEAR 当前日期是这一年的第几天
//DAY_OF_WEEK,用来获得当前日期是一周的第几天
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (int i = 0; i <calendar.get(Calendar.DAY_OF_WEEK)-1; i ) {
System.out.print("\t");
}
int days = calendar.getActualMaximum(Calendar.DATE); //获取当前月的最大天数
for (int i = 1; i <=days; i ) {
int nowDay = calendar.get(Calendar.DAY_OF_MONTH);
if(day == nowDay ){ //如果循环日期 等于输入日期 *号标出
System.out.print(calendar.get(Calendar.DAY_OF_MONTH) "*" "\t");
}else {//否则正常输出
System.out.print(calendar.get(Calendar.DAY_OF_MONTH) "\t");
}
if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){ //如果是周六换行
System.out.println();
}
calendar.add(Calendar.DAY_OF_MONTH,1); //每次 1
}
}
运行结果如下:
下面 大家来温故下 Calander 的知识
获取时间
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//获取时间:以目前系统时间 :2019:01:27 16:27:27 为例
@Test
public void testGetCalender() {
Calendar cal = Calendar.getInstance(); //Calendar类的实例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间的格式化
//当前系统时间
Date date = cal.getTime(); //date=Sun Jan 27 16:27:27 CST 2019
String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27
//当前年
int year = cal.get(Calendar.YEAR); //year=2019
//当前月 Calendar.MONTH从0开始 ,使用时通常会 1
int month = cal.get(Calendar.MONTH) 1; //month=1
//当前日:两种方法等价
int day_of_month = cal.get(Calendar.DAY_OF_MONTH); //day_of_month=27
int day = cal.get(Calendar.DATE); //day=27
//获取当月day的最大值!!
int max_day_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //max_day_of_month=31
//当前时钟:24小时制
int hour24 = cal.get(Calendar.HOUR_OF_DAY); //hour24=16
//当前时钟:12小时制
int hour12 = cal.get(Calendar.HOUR); //hour12=4
//当前:分钟
int minute = cal.get(Calendar.MINUTE); //minute=27
//当前秒
int second = cal.get(Calendar.SECOND); //second=27
//星期几:用数字(1~7)表示(星期日~星期六),使用时通常会-1
int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1; //day_of_week=0
//上午-0;下午-1
int amOrPm = cal.get(Calendar.AM_PM); //amOrPm=1
//当前年的第几周
int week_of_year = cal.get(Calendar.WEEK_OF_YEAR); //week_of_year=5
//当前月的星期数
int week_of_month = cal.get(Calendar.WEEK_OF_MONTH); //week_of_month=5
//当前月中的第几个星期
int day_of_week_in_month = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH); //day_of_week_in_month=4
//当前年的第几天
int day_of_year = cal.get(Calendar.DAY_OF_YEAR); //day_of_year=27
}
}
设置时间
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//设置时间:以目前系统时间 :2019:01:27 16:27:27 为例
@Test
public void testSetCalender() {
Calendar cal = Calendar.getInstance(); //Calendar类的实例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间的格式化
//1.当前系统时间
Date date = cal.getTime(); //data=Sun Jan 27 16:27:27 CST 2019
String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27
//2.设置时间:不设置项,默认Calendar已经存在日期
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, 30);
String date1 = sdf.format(cal.getTime()); //time=2019-01-27 11:30:30
//3.设置时间:全部设置,则输出自定义时间
cal.set(Calendar.YEAR, 2018);
cal.set(Calendar.MARCH, 7);
cal.set(Calendar.DATE, 21);
String date2 = sdf.format(cal.getTime()); //time=2018-08-21 11:30:30
//4.时间重置:Date date = new date();
String newDate1 = sdf.format(cal.getTime()); //newDate1=2018-08-21 11:30:30
String newDate2 = sdf.format(new Date()); //newDate2=2019:01:27 16:27:27
}
}
时间运算
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalenderTest {
//时间运算:以目前系统时间 :2019:01:27 16:27:27 为例
@Test
public void testAddCalender() {
Calendar cal = Calendar.getInstance(); //Calendar类的实例化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间的格式化
String nowTime = sdf.format(cal.getTime()); //nowTime=2019-01-27 16:27:27
cal.add(Calendar.YEAR, 1);
String time1 = sdf.format(cal.getTime()); //年:2020-01-27 16:27:27
cal.add(Calendar.MONTH, 2);
String time2 = sdf.format(cal.getTime()); //月:2020-03-27 16:27:27
cal.add(Calendar.DATE, 5);
String time3 = sdf.format(cal.getTime()); //日:2020-04-01 16:27:27
cal.add(Calendar.HOUR_OF_DAY, -1);
String time4 = sdf.format(cal.getTime()); //时:2020-04-01 15:27:27
cal.add(Calendar.MINUTE, 1);
String time5 = sdf.format(cal.getTime()); //分:2020-04-01 15:28:27
cal.add(Calendar.SECOND, 1);
String time6 = sdf.format(cal.getTime()); //秒:2020-04-01 15:27:28
}
}
上述总结的操作时间方法 不是自己敲得 转载于 点它
有问题评论区见 大家一起探讨呀 !!!