亲宝软件园·资讯

展开

java控制台实现可视化日历小程序

人气:0

废话不多说,直接上代码

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
 
/**
 * 可视化日历程序
 *
 * @author HQ
 * @e-mail ahmashq95@gmail.com
 * @date 2018/10/11.
 */
public class TestCalendar2 {
 public static void main(String[] args) throws ParseException {
 String string = "2018-10-11";
 
 
 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 Date date = dateFormat.parse(string);
 Calendar calendar = new GregorianCalendar();
 calendar.setTime(date);
 int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
 int day = calendar.get(Calendar.DAY_OF_MONTH);
 
 System.out.println("日\t一\t二\t三\t四\t五\t六");
 calendar.set(Calendar.DAY_OF_MONTH, 1);
 
 for (int i = 0; i < calendar.get(Calendar.DAY_OF_WEEK) - 1; i++) {
  System.out.print("\t");
 }
 
 for (int i = 1; i <= days; i++) {
  //判断是否为输入的时间是否为今天
  if(day==calendar.get(Calendar.DAY_OF_MONTH)){
  //如果是今天的话用红色标记
  System.out.print("\u001b[31m"+calendar.get(Calendar.DAY_OF_MONTH) + "\t"+"\u001b[0m");
  }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);
 }
 }
}

运行结果如图所示

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论