Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1560988
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 19:12:20

一起学习



/**

 * Title:

 * Description:  ots

 * Copyright:    Copyright (c) 2001

 * Company:      NMT

 * @author Colin

 * @version 1.0

 */

import java.sql.Timestamp;

import java.util.*;

import java.text.*;



public class FormatUtil {



    /**

	 * @param Calendar

	 * @return String 2001/12/13 Format

	 */

    public static String CalendarToStr(Calendar cal) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

        if (cal != null) {

            Date date = cal.getTime();

            return format.format(date);

        } else {

            return "";

        }

    }



    /**

	 * @param Calendar

	 * @return a Sunday Calendar in the Week

	 */

    public static Calendar starCalOfWeek(Calendar day) {

		int temp = day.get(Calendar.DAY_OF_WEEK);

        switch (temp) {

            case 1:

                return day;

            case 2:

                day.add(Calendar.DATE, -1);

                return day;

            case 3:

                 day.add(Calendar.DATE, -2);

                return day;

            case 4:

                 day.add(Calendar.DATE, -3);

                return day;

            case 5:

                 day.add(Calendar.DATE, -4);

                return day;

            case 6:

                 day.add(Calendar.DATE, -5);

                return day;

            case 7:

                 day.add(Calendar.DATE, -6);

                return day;

            default:

                return  day;;

        }

    }



    /**

	 * @param Calendar

	 * @return a Satday Calendar in the Week

	 */

    public static Calendar endCalOfWeek(Calendar day) {

		int temp = day.get(Calendar.DAY_OF_WEEK);

        switch (temp) {

            case 1:

                 day.add(Calendar.DATE, 6);

                return day;

            case 2:

                day.add(Calendar.DATE, 5);

                return day;

            case 3:

                day.add(Calendar.DATE, 4);

                return day;

            case 4:

                day.add(Calendar.DATE, 3);

                return day;

            case 5:

                day.add(Calendar.DATE, 2);

                return day;

            case 6:

                day.add(Calendar.DATE, 1);

                return day;

            case 7:

                return day;

            default:

                return day;;

        }

    }



	public static Date StrToDate(String str) {

        if(str.length() == 0) {

            return null;

        }

        int start = str.indexOf('/');

        String year = str.substring(0, start);

        start   ;

        int start1 = str.indexOf('/', start);

        String month = str.substring(start, start1);

        String day = str.substring(start1   1);

        Date date = new Date();

        Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        cal.set(Calendar.YEAR, Integer.parseInt(year));

        cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);

        cal.set(Calendar.DATE, Integer.parseInt(day));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        System.out.println(sdf.format(cal.getTime()));

        return cal.getTime();

	}



	public static Calendar StrToCalendar(String str) {

        if(str.length() == 0) {

            return null;

        }

        int start = str.indexOf('/');

        String year = str.substring(0, start);

        start   ;

        int start1 = str.indexOf('/', start);

        String month = str.substring(start, start1);

        String day = str.substring(start1   1);

        Date date = new Date();

        Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);

        cal.set(Calendar.DATE, Integer.parseInt(day));

        return cal;

	}



    /**

	 * @param Calendar

	 * @return String Sun Mon etc. Format

	 */

    public static String dayOfWeek(Calendar day) {

		int temp = day.get(Calendar.DAY_OF_WEEK);

        switch (temp) {

            case 1:

                return "Sun";

            case 2:

                return "Mon";

            case 3:

                return "Tue";

            case 4:

                return "Wed";

            case 5:

                return "Thu";

            case 6:

                return "Fri";

            case 7:

                return "Sat";

            default:

                return "";

        }

	}



    /**

	 * @param String Date Format 2001/12/13

	 * @return String Sun Mon etc. Format

	 */

    public static String dayOfWeek(String inDay) {

        Calendar cal = Calendar.getInstance();



        cal.setTime(StrToDate(inDay));

		int temp = cal.get(Calendar.DAY_OF_WEEK);

        switch (temp) {

            case 1:

                return "Sun";

            case 2:

                return "Mon";

            case 3:

                return "Tue";

            case 4:

                return "Wed";

            case 5:

                return "Thu";

            case 6:

                return "Fri";

            case 7:

                return "Sat";

            default:

                return "";

        }

	}



    public static Timestamp StrToTimestamp(String timestampStr,String pattern)

            throws ParseException {

        java.util.Date date = null;

        SimpleDateFormat format = new SimpleDateFormat(pattern);

        try {

            date = format.parse(timestampStr);

        } catch (ParseException e) {

                throw e;

        }

        return date == null ? null : new Timestamp(date.getTime());

    }



}
下载本文示例代码


关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类关于日期格式转换类
阅读(193) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~