• Calendar 实现日历实例


    import java.text.ParseException;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.Scanner;
    public class TestCalendar2 {
        public static void main(String[] args) throws ParseException {
            System.out.println("请输入日期(格式为:2010-3-3):");
            Scanner scanner = new Scanner(System.in);
            String dateString = scanner.nextLine(); // 2010-3-1
            // 将输入的字符串转化成日期类
            System.out.println("您刚刚输入的日期是:" + dateString);
            String[] str = dateString.split("-");
            int year = Integer.parseInt(str[0]);
            int month = new Integer(str[1]);
            int day = new Integer(str[2]);
            Calendar c = new GregorianCalendar(year, month - 1, day); // Month:0-11
            // 大家自己补充另一种方式:将字符串通过SImpleDateFormat转化成Date对象,
            //再将Date对象转化成日期类
            // SimpleDateFormat sdfDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            // Date date = sdfDateFormat.parse(dateString);
            // Calendar c = new GregorianCalendar();
            // c.setTime(date);
            // int day = c.get(Calendar.DATE);
            c.set(Calendar.DATE, 1);
            int dow = c.get(Calendar.DAY_OF_WEEK); // week:1-7 日一二三四五六
            System.out.println("日	一	二	三	四	五	六");
            for (int i = 0; i < dow - 1; i++) {
                System.out.print("	");
            }
            int maxDate = c.getActualMaximum(Calendar.DATE);
            // System.out.println("maxDate:"+maxDate);
            for (int i = 1; i <= maxDate; i++) {
                StringBuilder sBuilder = new StringBuilder();
                if (c.get(Calendar.DATE) == day) {
                    sBuilder.append(c.get(Calendar.DATE) + "*	");
                } else {
                    sBuilder.append(c.get(Calendar.DATE) + "	");
                }
                System.out.print(sBuilder);
                // System.out.print(c.get(Calendar.DATE)+
                //                ((c.get(Calendar.DATE)==day)?"*":"")+"	");
     
                if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
                    System.out.print("
    ");
                }
                c.add(Calendar.DATE, 1);
            }
        }
    }
    

      

  • 相关阅读:
    ibatis resultMap 结果集映射
    iabtis初探
    struts2获取请求参数的三种方式及传递给JSP参数的方式
    Struts2的运行机制简介
    Spring AOP面向切面编程的实现
    java 单例模式及getInstance的好处
    It is indirectly referenced from required .class files
    AngularJS学习篇(二)
    AngularJS学习篇(一)
    Flex布局语法
  • 原文地址:https://www.cnblogs.com/leigepython/p/10483936.html
Copyright © 2020-2023  润新知