首先写了一个公用的转换帮助类 package com.dzf.test;import java.math.BigDecimal;import java.sql.Date;import java.text.DecimalFormat;import java.text.ParseException;import java.text.SimpleDateFormat;public class ConvertHelper ...{ public static final String DecimalFormat = "###,###,###,##0.00"; public static final String DateFormat = "yyyy-MM-dd"; /** *//** * 把字符串转换为整数。 * * @param str * 待转换的字符串 * @return 整数 * @throws Exception * 如果字符串格式不对 */ public static int StringToInt(String str) ...{ if (str == null || str.equals("")) ...{ return 0; } else ...{ return Integer.parseInt(str); } } /** *//** * 把字符串转换为长整形。 * * @param str * 待转换的字符串 * @return 整数 * @throws Exception * 如果字符串格式不对 */ public static long StringToLong(String str) ...{ if (str == null || str.equals("")) return 0; return StringToNumber(str).longValue(); } /** *//** * 把字符串转换为DOUBLE。 * * @param str * 待转换的字符串 * @return 整数 * @throws Exception * 如果字符串格式不对 */ public static double StringToDouble(String str) ...{ if (str == null || str.equals("")) return 0; return StringToNumber(str).doubleValue(); } /** *//** * 把字符串转换为Decimal。 * * @param str * 待转换的字符串 * @return 整数 * @throws Exception * 如果字符串格式不对 */ public static BigDecimal StringToDecimal(String str) ...{ return new BigDecimal(StringToNumber(str).doubleValue()); } /** *//** * 把字符串转换为NUMBER。 * * @param str * 待转换的字符串 * @return 整数 * @throws Exception * 如果字符串格式不对 */ public static Number StringToNumber(String str) ...{ if (str == null) return null; if (str.equals("")) str = "0"; Number number; DecimalFormat df = new DecimalFormat(DecimalFormat); try ...{ number = df.parse(str); return new BigDecimal(number.toString()); } catch (ParseException e) ...{ e.printStackTrace(); } return null; } /** *//** * 把BigDecimal类型转换为字符串 * * @param number * @return */ public static String DecimalToString(BigDecimal number) ...{ if (number == null) ...{ return "0.00"; } DecimalFormat df = new DecimalFormat(DecimalFormat); return df.format(number); } /** *//** * 把Number类型转换为字符串 * * @param number * @return */ public static String DecimalToString(Number number) ...{ if (number == null) ...{ return "0.00"; } DecimalFormat df = new DecimalFormat(DecimalFormat); return df.format(number); } /** *//** * 把double类型转换为字符串 * * @param number * @return */ public static String DecimalToString(double number) ...{ DecimalFormat df = new DecimalFormat(DecimalFormat); return df.format(number); } /** *//** * 把long类型转换为字符串 * * @param number * @return */ public static String DecimalToString(long number) ...{ DecimalFormat df = new DecimalFormat(DecimalFormat); return df.format(number); } /** *//** * 把java.util.Date类型转换为字符串 * * @param date * @return */ public static String DateToString(java.util.Date date) ...{ if (date == null) return ""; SimpleDateFormat sdf = new SimpleDateFormat(DateFormat); return sdf.format(date); } /** *//** * 把字符串转换为Date类型 * * @param str * 时间类型如"2007-10-28" * @return * @throws Exception */ public static Date StringToDate(String str) ...{ if (str == null || str.equals("")) return null; SimpleDateFormat sdf = new SimpleDateFormat(DateFormat); Date date; try ...{ date = new Date(sdf.parse(str).getTime()); return date; } catch (ParseException e) ...{ e.printStackTrace(); } return null; } /** *//** * 把字符串转换为Date类型 * * @param str * 时间类型如"2007-10-28" * @param format * 时间格式如"yyyy-MM-dd" * @return * @throws Exception */ public static Date StringToDate(String str, String format) ...{ if (str == null || str.equals("")) return null; SimpleDateFormat sdf = new SimpleDateFormat(format); Date date; try ...{ date = new Date(sdf.parse(str).getTime()); return date; } catch (ParseException e) ...{ e.printStackTrace(); } return null; } /** *//** * 将OBJECT转换为STRING * * @param obj * @return 如果OBJ为NULL, 则返回null */ public static String ObjToString(Object obj) ...{ return ObjToString(obj, null); } /** *//** * 将OBJECT转换为STRING * * @param obj * @param retStr * 为空时的返回值 * @return 如果OBJ为NULL, 则返回retStr */ public static String ObjToString(Object obj, String retStr) ...{ if (obj == null) ...{ return retStr; } return obj.toString(); } /** *//** * 将传入的INT值,装换为固定字符补齐的字符串 * * @param value * INT值 * @param num * 长度 * @param vchar * 字符 * @return */ public static String IntToString(int value, int num, char vchar) ...{ StringBuffer result = new StringBuffer(); result.append(new Integer(value).toString()); while (num > result.length()) ...{ result.insert(0, vchar); } return result.toString(); } public static void main(String[] args) ...{ try ...{ System.out.print(ConvertHelper.StringToNumber("1.0")); } catch (Exception e) ...{ // TODO Auto-generated catch block e.printStackTrace(); } }} 时间处理 package com.dzf.test;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.Iterator;import java.util.List;public class DateUtil ...{ /** *//** * 判断输入年份是否当前年份 * * @param year * 输入年份 * @return true - 是 false - 不是 */ public static boolean isCurrentYear(String year) ...{ Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy"); String strDate = df.format(date); if (strDate.equals(year)) ...{ return true; } return false; } /** *//** * 判断输入月份是否当前月份 * * @param year * 输入月份 * @return true - 是 false - 不是 */ public static boolean isCurrentMonth(String month) ...{ Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat("MM"); String strDate = df.format(date); if (strDate.equals(month)) ...{ return true; } return false; } /** *//** * 获取输入日期的月份 * * @param date * @return */ public static String getMonth(Date date) ...{ SimpleDateFormat df = new SimpleDateFormat("MM"); String strDate = df.format(date); return strDate; } /** *//** * 获取输入日期的月份 * * @param strDate * @return * @throws Exception */ public static String getMonth(String startDate) throws Exception ...{ Date date = ConvertHelper.StringToDate(startDate); return getMonth(date); } /** *//** * 获取输入日期的年份 * * @param date * @return */ public static String getYear(Date date) ...{ SimpleDateFormat df = new SimpleDateFormat("yyyy"); String startDate = df.format(date); return startDate; } /** *//** * 获取输入日期的年份 * * @param startDate * @return * @throws Exception */ public static String getYear(String startDate) throws Exception ...{ Date date = ConvertHelper.StringToDate(startDate); return getYear(date); } /** *//** * 根据输入的某天,返回该月第一天或者最后一天 * * @param startDate * 某天 * @param flag * 0 - 第一天 1 - 最后一天 * @return * @throws Exception */ public static String getFirstOrLastDay(Date date, int flag) throws Exception ...{ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM"); String startDate = df.format(date) + "-01"; // 取第一天 if (flag == 0) ...{ return startDate; } Date firstdate = ConvertHelper.StringToDate(startDate); // 取最后一天 Calendar calendar = Calendar.getInstance(); calendar.setTime(firstdate); calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.DAY_OF_YEAR, -1); df = new SimpleDateFormat("yyyy-MM-dd"); return df.format(calendar.getTime()); } /** *//** * 根据输入的某天,返回该月的最后一天 * * @param startDate * 某天 * @return * @throws Exception */ public static String getLastDay(Date date) throws Exception ...{ return getFirstOrLastDay(date, 1); } /** *//** * 根据输入的某天,返回该月第一天或者最后一天 * * @param startDate * 某天 * @param flag * 0 - 第一天 1 - 最后一天 * @return * @throws Exception */ public static String getFirstOrLastDay(String startDate, int flag) throws Exception ...{ Date date = ConvertHelper.StringToDate(startDate); return getFirstOrLastDay(date, flag); } /** *//** * 根据输入的某天,返回该月最后一天 * * @param startDate * 某天 * @return * @throws Exception */ public static String getLastDay(String startDate) throws Exception ...{ Date date = ConvertHelper.StringToDate(startDate); return getFirstOrLastDay(date, 1); } /** *//** * 根据输入的年、月,返回该月第一天或者最后一天 * * @param year * 年 * @param month * 月 * @param flag * 0 - 第一天 1 - 最后一天 * @return * @throws Exception */ public static String getFirstOrLastDay(String year, String month, int flag) throws Exception ...{ String startDate = year + "-" + month + "-01"; return getFirstOrLastDay(startDate, flag); } /** *//** * 根据输入的年、月,返回该月最后一天 * * @param year * 年 * @param month * 月 * @return * @throws Exception */ public static String getLastDay(String year, String month) throws Exception ...{ // 取当月最后一天 return getFirstOrLastDay(year, month, 1); } /** *//** * 根据本月日期,获取上月最后一天日期 * * @param date * 本月任意一天 * @return 上月最后一天 * @throws Exception */ public static String getSyLastDay(Date date) throws Exception ...{ // 取本月第一天 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM"); String startDate = df.format(date); startDate = startDate + "-01"; Date firstDate = ConvertHelper.StringToDate(startDate); // 取上月最后一天 Calendar calendar = Calendar.getInstance(); calendar.setTime(firstDate); calendar.add(Calendar.DAY_OF_YEAR, -1); SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd"); return dff.format(calendar.getTime()); } /** *//** * 根据本月日期,获取上月最后一天日期 * * @param startDate * 本月任意一天 * @return 上月最后一天 * @throws Exception */ public static String getSyLastDay(String startDate) throws Exception ...{ return getSyLastDay(ConvertHelper.StringToDate(startDate)); } /** *//** * 获取传入日期相邻第N个月的第一天或者最后一天 * * @param date * @param nearby * 相邻月份 - 2 当前日期之后2个月 -2 - 当前日期之前2个月 * @param flag * 0 - 第一天 1 - 最后一天 * @return 相邻第N个月的第一天或者最后一天 * @throws Exception */ public static String getNearbyMonthFirstOrLastDay(Date date, int nearby, int flag) throws Exception ...{ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM"); String startDate = df.format(date) + "-01"; Date firstdate = ConvertHelper.StringToDate(startDate); Calendar calendar = Calendar.getInstance(); calendar.setTime(firstdate); // 取第一天 if (flag == 0) ...{ calendar.add(Calendar.MONTH, nearby); } else ...{ // 取最后一天 calendar.add(Calendar.MONTH, nearby + 1); calendar.add(Calendar.DAY_OF_YEAR, -1); } df = new SimpleDateFormat("yyyy-MM-dd"); return df.format(calendar.getTime()); } /** *//** * 获取传入日期相邻第N个月的第一天或者最后一天 * * @param startDate * @param nearby * 相邻月份 - 2 当前日期之后2个月 -2 - 当前日期之前2个月 * @param flag * 0 - 第一天 1 - 最后一天 * @return 相邻第N个月的第一天或者最后一天 * @throws Exception */ public static String getNearbyMonthFirstOrLastDay(String startDate, int nearby, int flag) throws Exception ...{ return getNearbyMonthFirstOrLastDay( ConvertHelper.StringToDate(startDate), nearby, flag); } /** *//** * 获取传入日期相邻第N个天的日期 * * @param date * @param nearby * 相邻天数 2 当前日期之后2天 -2 - 当前日期之前2天 * @return 相邻第N个天的日期 */ public static String getNearbyDay(Date date, int nearby) ...{ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, nearby); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); return df.format(calendar.getTime()); } /** *//** * 获取传入日期相邻第N个天的日期 * * @param startDate * @param nearby * 相邻天数 2 当前日期之后2天 -2 - 当前日期之前2天 * @return 相邻第N个天的日期 * @throws Exception */ public static String getNearbyDay(String startDate, int nearby) throws Exception ...{ return getNearbyDay(ConvertHelper.StringToDate(startDate), nearby); } /** *//** * 获取某年某月的天数 * * @param year * 年份 * @param month * 月份 * @return days 本月份的天数 */ /**//* * public static int getMonthDays(int year, int month) { Calendar c1 = * Calendar.getInstance(); c1.set(year, month - 1, 1); Calendar c2 = * Calendar.getInstance(); c2.set(year, month, 1); long c2s = * c2.getTimeInMillis(); long c1s = c1.getTimeInMillis(); long day = 1 * 24 * * 60 * 60 * 1000; int days = (int) ((c2s - c1s) / day); // * System.out.println(year + "年" + month + "月有" + days + "天"); return days; } */ public static int getMonthDays(int year, int month) ...{ Calendar c = Calendar.getInstance(); c.clear(); c.add(Calendar.YEAR, year); c.add(Calendar.MONTH, month); int days = c.getActualMaximum(Calendar.DAY_OF_MONTH); // System.out.println(year + "年" + month + "月有" + days + "天"); return days; } /** *//** * 获取两个时间段的天数 * * @param startDate * 开始时间 * @param endDate * 结束时间 * @return days 返回两个时间段的天数 */ public static int getDaysBetween(String startDate, String endDate) ...{ Calendar d1 = Calendar.getInstance(); Calendar d2 = Calendar.getInstance(); d1.clear(); d2.clear(); try ...{ d1.setTime(ConvertHelper.StringToDate(startDate)); d2.setTime(ConvertHelper.StringToDate(startDate)); } catch (Exception e) ...{ e.printStackTrace(); } if (d1.after(d2)) ...{ java.util.Calendar swap = d1; d1 = d2; d2 = swap; } int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR); int y2 = d2.get(Calendar.YEAR); if (d1.get(Calendar.YEAR) != y2) ...{ d1 = (Calendar) d1.clone(); do ...{ days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数 d1.add(Calendar.YEAR, 1); } while (d1.get(Calendar.YEAR) != y2); } // System.out.println(startDate + "至" + endDate + "有" + days + "天"); return days; } /** *//** * 获取增加月数后的月份 * * @param startDate * 开始时间 * @param monthNum * 增加的月数 * @return */ public static String getEndDate(String startDate, int monthNum) ...{ String resultDate; resultDate = ""; try ...{ Calendar calendar = Calendar.getInstance(); calendar.setTime(ConvertHelper.StringToDate(startDate)); // calendar.add(Calendar.YEAR, monthNum); calendar.add(Calendar.MONTH, monthNum); calendar.add(Calendar.DATE, -1); Date date = calendar.getTime(); resultDate = ConvertHelper.DateToString(date); } catch (Exception e) ...{ e.printStackTrace(); } // System.out.print(startDate+"增加"+monthNum+"月后的日期为"+resultDate); return resultDate; } /** *//** * 获取某年第几个星期星期几的日期 * * @param year * 年份 * @param weekOfYear * 第几个星期 * @param DayOfWeek * 星期几 * @return 日期 */ public static String getDayOfWeek(String year, String weekOfYear, String DayOfWeek) ...{ String date; date = ""; Calendar cal = Calendar.getInstance(); try ...{ cal.set(Calendar.YEAR, ConvertHelper.StringToInt(year)); cal.set(Calendar.WEEK_OF_YEAR, ConvertHelper .StringToInt(weekOfYear)); cal.set(Calendar.DAY_OF_WEEK, ConvertHelper.StringToInt(DayOfWeek)); date = ConvertHelper.DateToString(cal.getTime()); } catch (Exception e) ...{ e.printStackTrace(); } // System.out.print(year+"年的第"+weekOfYear+"个星期的日期为"+date); return date; } /** *//** * 获取日期为第几个星期 * * @param startDate * @return */ public static int getWeekOfYear(String startDate) ...{ int resultWeek = 0; try ...{ Calendar c = Calendar.getInstance(); c.setTime(ConvertHelper.StringToDate(startDate)); resultWeek = c.get(Calendar.WEEK_OF_YEAR); // System.out.print(startDate+"为"+c.get(Calendar.YEAR)+"的第"+resultWeek+"个星期"); } catch (Exception e) ...{ e.printStackTrace(); } return resultWeek; } /** *//** * 获取某年某月所有星期的开始时间跟结束时间 * @param year * @param month * @return */ public static List<String> getAllWeekOfMonth(String year, String month) ...{ // SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); List<String> timeList; try ...{ String last; last = DateUtil.getLastDay(year, month); timeList = new ArrayList<String>(); // 一个星期的毫秒数 long time = 7 * 3600 * 24 * 1000; // 一个月开始 Date firstDay = ConvertHelper.StringToDate(year + "-" + month + "-01"); // 月的最后一天 Date lastDay = ConvertHelper.StringToDate(last); // 最后一天的毫秒 long endmonth = lastDay.getTime(); Calendar calendar = Calendar.getInstance(); // 得到月开始的那天所在周的周一日期 calendar.setTime(firstDay); calendar.set(Calendar.DAY_OF_WEEK, 2); Date monday = calendar.getTime(); String mon = ConvertHelper.DateToString(monday); // 得到月开始的那天所在周的周日日期 calendar.set(Calendar.DAY_OF_WEEK, 1); Date sunday = calendar.getTime(); // 由于系统要求一周是从周一开始周日结束 所以得到下周的周日 calendar.setTimeInMillis(sunday.getTime() + time); sunday = calendar.getTime(); String sun = ConvertHelper.DateToString(calendar.getTime()); timeList.add(mon + "," + sun); // 循环得到本月的周 for (int i = 0; i < 6; i++) ...{ calendar.setTimeInMillis(monday.getTime() + time); monday = calendar.getTime(); mon = ConvertHelper.DateToString(monday); calendar.setTimeInMillis(sunday.getTime() + time); sunday = calendar.getTime(); sun = ConvertHelper.DateToString(sunday); if (monday.getTime() <= endmonth) ...{ timeList.add(mon + "," + sun); } else ...{ break; } } return timeList; } catch (Exception e) ...{ e.printStackTrace(); } return null; } public static void main(String[] args) ...{ List timeList = DateUtil.getAllWeekOfMonth("2008", "04"); for (Iterator iter = timeList.iterator(); iter.hasNext();) ...{ String element = (String) iter.next(); System.out.println(element); } }}