继往开来 吐故纳新
日历
网志分类
· 所有网志 (1031)
· 个人作品 (64)
· 软件设计 (33)
· 面向对象编程 (22)
· JavaAPI (44)
· Java开源工具 (36)
· Swing (34)
· Java语法细节 (39)
· 样式表CSS (12)
· XML (9)
· J2EE(JavaEE) (25)
· 算法数据结构 (64)
· 正则表达式 (4)
· 软件知识 (6)
· Java线程 (9)
· Web开发.Jsp/Servlet/Struts (20)
· 程序随想录 (7)
· Hibernate (7)
· Spring (5)
· J2SE 高级 (2)
· J2SE 高级 (0)
· Web开发.Ajax (15)
· Web开发.JavaScript (48)
· DB4O (2)
· Web开发.CSS/Html (22)
· C# (20)
· ERP (4)
· JDBC (1)
· 编程资源 (16)
· 编程感悟 (29)
· DB/Sql (13)
· VB (29)
· VC (2)
· 桌面脚本 (3)
· 新兴软件 (3)
· 英语学习 (21)
· 网文转载 (164)
· 职场风云 (40)
· 诗词歌赋 (32)
· 生活感言 (79)
· 生活常识 (2)
· 奇文共赏 (14)
· 财经纵横 (11)
· 未分类 (19)
站内搜索
友情链接
· 歪酷博客
· 我的歪酷 非非共享界
· 偶要雷锋
· 豆瓣
· nczonline
· 当当网
· easyjf中文站
· Donews
· 天极Java文章列表
· W3CSchool
· taiten的BLOG
· Dojo中国
· Dojo
· Extjs.com
· Lifehack中文网志
· JaveEye的一个AS专题
· Banq's JDon
· Java 中文网址大全
· 梦想Java
· 360Doc个人图书馆
· java开源大全
· 我在硅谷动力的软件下载站
· 站长中国
· 随意贴
· CSS教学素材站
· java 参考中文站
· 面向构件与SOA社区
· 彩字生成
· 派派小说论坛
· 如坐春风
· 英语学习网
· BBC CHina
· www.dlbang.com
· 古文竖排格式在线转化工具
· 免费家谱
· 图片上传基地
· 风景壁纸
· 和风细雨
· MyC#BlogInCsdn

订阅 RSS

0391852

歪酷博客

开此博一为经验积累,二为资料收集,三为同道交流,四为资源共享.
« 上一篇: 【转载】皇帝的嗜好漫议 下一篇: 【转载】计算机科学与技术学习反思录 »
Junglesong @ 2008-07-20 00:05

package com.sitinspring.datetime;

import java.util.ArrayList;
import java.util.List;


public class MonthlyCalendar{
    
private static final String tab = "\t";

    
// 日历的年月
    private String yearMonth;
    
    
// 一个年月的日期
    private List<String> days;
    
    
public MonthlyCalendar(String yearMonth){
        
this.yearMonth=yearMonth;
        fillDays();
    }

    
    
private void fillDays(){
        days
=new ArrayList<String>();
        
        
// 每个月第一日前的空白
        int spacesBeforeFirstDay=DateTimeUtil.getWeekOfFirstDay(yearMonth);
        
for(int i=0;i<spacesBeforeFirstDay;i++){
            days.add(tab);
        }

        
        
// 每个月的日期
        int dayCountInaMonth=DateTimeUtil.getDaysInAMonth(yearMonth);
        
for(int i=0;i<dayCountInaMonth;i++){
            days.add((i
+1)+tab);
        }
        
    }

    
    
public String getText(){
        StringBuffer sb
=new StringBuffer();
        
        sb.append(
"==="+yearMonth+"===\r\n");
        sb.append(
"--------------------------------------------------\r\n");
        
for(int i=0;i<days.size();i++){
            sb.append(days.get(i));
            
if((i+1% 7==0){
                sb.append(
"\r\n");
            }

        }

        sb.append(
"\r\n--------------------------------------------------\r\n");
        
        
return sb.toString();
    }

    
    
public static void main(String[] args){
        System.out.println(
new MonthlyCalendar("2008.8").getText());
    }

}


package com.sitinspring.datetime;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class CalendarMaker {
    
public CalendarMaker(int year) {
        
this(String.valueOf(year));
    }


    
public CalendarMaker(String year) {
        
// 生成日历文件
        makeFile(year);
    }


    
private void makeFile(String fileName) {
        
try {
            BufferedWriter out 
= new BufferedWriter(new FileWriter(fileName
                    
+ ".txt"));

            
for (int i = 1; i < 13; i++{
                out.write(
new MonthlyCalendar(fileName + "." + i).getText()); // 将字符串“内容”写入文件
            }


            out.close();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

    }


    
public static void main(String[] args) {
        
new CalendarMaker(2008);
    }

}


package com.sitinspring.datetime;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期时间处理实用类
 * 
 * 
@author sitinspring(junglesong@gmail.com)
 * 
@since 2008-7-18 上午10:30:15
 * @vsersion 1.00 创建 sitinspring 2008-7-18 上午10:30:15
 
*/

public final class DateTimeUtil {
    
private DateTimeUtil() {

    }


    
/**
     * 以格式format返回表示日期时间的字符串
     * 
     * 
@param format
     * 
@return
     
*/

    
public static String getDateTimeStr(String format) {
        Date date 
= new Date();
        Format formatter 
= new SimpleDateFormat(format);
        
return formatter.format(date);
    }


    
/**
     * 取得当前日期时间
     * 
     * 
@return
     
*/

    
public static String getCurrDateTime() {
        
return getDateTimeStr("yyyy.MM.dd HH:mm:ss");
    }


    
/**
     * 取得当前日期,不足两位前补零
     * 
     * 
@return
     
*/

    
public static String getCurrDate() {
        
return getDateTimeStr("yyyy.MM.dd");
    }


    
/**
     * 取得当前日期
     * 
     * 
@return
     
*/

    
public static String getSimpleCurrDate() {
        
return getDateTimeStr("yyyy.M.d");
    }


    
/**
     * 取得当前时间
     * 
     * 
@return
     
*/

    
public static String getCurrTime() {
        
return getDateTimeStr("HH:mm:ss");
    }


    
/**
     * 取得当前年月
     * 
     * 
@return
     
*/

    
public static String getCurrYearMonth() {
        
return getDateTimeStr("yyyy.MM");
    }


    
/**
     * 从文本形式日期取得Date日期时间
     * 
     * 
@param strMonth
     * 
@return
     
*/

    
private static Date getDate(String strMonth) {
        SimpleDateFormat myFormatter 
= new SimpleDateFormat("yyyy.MM.dd");

        
try {
            java.util.Date date 
= myFormatter.parse(strMonth);
            
return date;
        }
 catch (Exception ex) {
            
return null;
        }

    }


    
/**
     * 得到两个文本日期之间的天数
     * 
     * 
@param startDate
     * 
@param endDate
     * 
@return
     
*/

    
public static long getDaysBetween(String startDate, String endDate) {
        Date dStart 
= getDate(startDate);
        Date dEnd 
= getDate(endDate);

        
return (dEnd.getTime() - dStart.getTime()) / (24 * 60 * 60 * 1000);
    }


    
/**
     * 取某月的天数,strMonth的格式是"yyyy.MM"
     * 
@param strMonth
     * 
@return
     
*/

    
public static int getDaysInAMonth(String strMonth) {
        String[] arr 
= strMonth.split("[.]");

        
// Create a calendar object of the desired month
        Calendar cal = new GregorianCalendar(Integer.parseInt(arr[0]), Integer
                .parseInt(arr[
1]) - 11);

        
// Get the number of days in that month
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        
return days;
    }


    
/**
     * 取某月第一天是周几,strMonth的格式是"yyyy.MM"
     * 
@param strMonth
     * 
@return
     
*/

    
public static int getWeekOfFirstDay(String strMonth) {
        String[] arr 
= strMonth.split("[.]");

        Calendar xmas 
= new GregorianCalendar(Integer.parseInt(arr[0]), Integer
                .parseInt(arr[
1]) - 11);
        
int dayOfWeek = xmas.get(Calendar.DAY_OF_WEEK) - 1;
        
return dayOfWeek;
    }


    
public static void main(String[] args) {
        System.out.println(
"当前日期时间为:" + getCurrDateTime());
        System.out.println(
"当前日期为:" + getCurrDate());
        System.out.println(
"当前日期为:" + getSimpleCurrDate());
        System.out.println(
"当前时间为:" + getCurrTime());
        System.out.println(
"2008.07.05与2008.07.18之间相隔:"
                
+ getDaysBetween("2008.07.05""2008.07.18"+ "");
        System.out.println(
"当前年月为:" + getCurrYearMonth());
        System.out.println(
"本月第一天为周" + getWeekOfFirstDay(getCurrYearMonth()));
        System.out.println(
"本月有" + getDaysInAMonth(getCurrYearMonth()) + "");
    }

}


评论 / 个人网页 / 扔小纸条
*昵称

已经注册过? 请登录

Email
网址
*评论