| 日历 |
|
|
| 网志分类 |
|
|
|
| 站内搜索 |
|
| 友情链接 |
|
0391852
|
|
| 开此博一为经验积累,二为资料收集,三为同道交流,四为资源共享. |
|
|
|
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]) - 1, 1);

// 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]) - 1, 1);
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()) + "天");
}
}
|
|
|