码迷,mamicode.com
首页 > 编程语言 > 详细

java日历

时间:2014-12-27 15:12:44      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

//日历源代码
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;



public class Calendar1 {
    
    public  void show(String temp){
        Date d = new Date(); 
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
         String dateNowStr = sdf.format(d);  
         if(temp==null) temp=dateNowStr;
         calen(temp);
    }
    public static  void calen(String temp) {

         DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date=format.parse(temp);
            
                Calendar calendar=new GregorianCalendar();
                calendar.setTime(date);
                int day=calendar.get(calendar.DATE)-1;
                calendar.set(calendar.DATE, 1);
                int oneday=calendar.get(calendar.DAY_OF_WEEK);
                
                
                int maxDay=calendar.getActualMaximum(calendar.DATE);
                System.out.println(temp);
                
                System.out.println("日\t一\t二\t三\t四\t五\t六");
                
                for(int j=0;j<oneday-1;j++)
                    System.out.print(" \t");
                for (int i = 0; i < maxDay; i++) {
                    if(i==day){
                        System.out.print("*");
                    }
                    System.out.print(i+1+"\t");
                    
                    int w=calendar.get(calendar.DAY_OF_WEEK);
                    if(w==calendar.SATURDAY)System.out.println();
                    calendar.add(calendar.DATE, 1);
                }
                
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
    }
    
    
    public static void main(String[] args) {
        Calendar1 test1=new Calendar1();
        test1.show(null);
        while(true){
        System.out.println("\n请输入日期:(格式)XXXX-XX-XX");
        Scanner scanner=new Scanner(System.in);
        String inputstr= scanner.nextLine();
        System.out.println(inputstr);
        if(inputstr.matches("^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$"))
        test1.show(inputstr);
        else System.out.println("输入错误,请重新输入");}
        
        
    }
}

 

java日历

标签:

原文地址:http://www.cnblogs.com/oumyye/p/4188361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!