/**
*<p>日期通用类,部分代码来源于网友,自己通过整理以备使用</p>
*整理者: Qingbao-gao
*Blong:http://www.blogjava.net/1504
*E-mail:yyk1504@163.com
*QQ:251218333
*<p>2009-3-6 下午02:15:08</p>
*/
package com.hjt.tcmsp.common;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DateUtil
{
/**
* <p>获得时间对应的星期数</p>
* @param dateString
*/
public static void getWeek(String dateString)
{
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五","星期六" };
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar calendar = Calendar.getInstance();
@SuppressWarnings("unused")
Date date = new Date();
try
{
date = sdfInput.parse(dateString);
}
catch (ParseException e)
{
e.printStackTrace();
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
if(dayOfWeek<0)dayOfWeek=0;
System.out.println(dayNames[dayOfWeek]);
}
/**
* <p>以基准日期的天数获得时间对应的星期数</p>
* @param dateString
*/
public static void getWeek2(int year,int month,int day)
{
int count=0,i=1,j=1970;
while(j<year)
{
if(j%4==0&&j%100!=0||j%400==0) count+=366;
else count+=365;
j++;
}
while(i<month)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) count+=31;
if(i==4||i==6||i==9||i==11) count+=30;
if(i==2)
if(year%4==0&&year%100!=0||year%400==0) count+=29;
else count+=28;
i++;
}
count+=day;
switch(count%7)
{
case 1:System.out.println("星期四");break;
case 2:System.out.println("星期五");break;
case 3:System.out.println("星期六");break;
case 4:System.out.println("星期天");break;
case 5:System.out.println("星期一");break;
case 6:System.out.println("星期二");break;
case 0:System.out.println("星期三");break;
}
}
/**
* <p>根据日期返回对应的星期</p>
* @param date
* @return 星期
*/
public static String getFormatDateWeek(Date date)
{
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE");
return bartDateFormat.format(date);
}
/**
* <p>获得指定格式的时间一周时间,按顺序从周一到周日</p>
* @param dateFormatStr
* @return 一周时间(中国人习惯)
*/
@SuppressWarnings("static-access")
public static String[] getWeekOfDayChina(String dateFormatStr)
{
String[] arr=new String[7];
Date date=null;
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
sdf.applyPattern(dateFormatStr);
for(int i=1,k=0;i<8;i++,k++)
{
calendar.set(Calendar.DAY_OF_WEEK,i);
if(k>0)
{
date=calendar.getTime();
String timeStr = sdf.format(date);
arr[i-2]=timeStr;
calendar.setTime(date);
}
}
calendar.add(Calendar.DATE,1);
date=calendar.getTime();
String timeStr = sdf.format(date);
arr[6]=timeStr;
return arr;
}
/**
* <p>获得指定格式的时间一周时间,按顺序从周日到周六</p>
* @param dateFormatStr
* @return 一周时间(西方习惯)
*/
@SuppressWarnings("static-access")
public static String[] getWeekOfDayEnlish(String dateFormatStr)
{
String[] arr=new String[7];
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
sdf.applyPattern(dateFormatStr);
int []week={Calendar.SUNDAY,Calendar.MONDAY,Calendar.TUESDAY,Calendar.WEDNESDAY,Calendar.THURSDAY,Calendar.FRIDAY,Calendar.SATURDAY};
for(int i=0;i<week.length;i++)
{
Calendar c = Calendar.getInstance();
Date date=new Date();
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK,week[i]);
String timeStr=new SimpleDateFormat(dateFormatStr).format(c.getTime());
arr[i]=timeStr;
}
return arr;
}
/**
* <p>获得时间对应的星期数</p>
* @param dateString
*/
public static String getWeeks(String inputTimeStr)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
@SuppressWarnings("unused")
Date date = null;
try
{
date = sdf.parse(inputTimeStr);
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE");
return bartDateFormat.format(date);
}
/**
* <p>返回指定格式的第n天后的时间串</p>
* @param date
* @param n
* @param dateFormatStr
* @return n天后的时间串
*/
public static String afterNDay(Date date,int n,String dateFormatStr)
{
Calendar c=Calendar.getInstance();
DateFormat df=new SimpleDateFormat(dateFormatStr);
c.setTime(date);
c.add(Calendar.DATE,n);
Date d=c.getTime();
String s=df.format(d);
return s;
}
/**
* <p>返回指定格式的第n天后的时间串集合</p>
* @param date
* @param n
* @param dateFormatStr
* @return n天后的时间串
*/
public static String[] getAfterNDay(Date date,int n,String dateFormatStr)
{
Calendar c=Calendar.getInstance();
String []st=new String[n];
DateFormat df=new SimpleDateFormat(dateFormatStr);
for(int i=0;i<n;i++)
{
c.setTime(date);
c.add(Calendar.DATE,i);
Date d=c.getTime();
String s=df.format(d);
st[i]=s;
}
return st;
}
/**
* <p>返回指定格式的第n天前的时间串集合</p>
* @param date
* @param n
* @param dateFormatStr
* @return n天后的时间串
*/
public static String[] getBeforNDay(Date date,int n,String dateFormatStr)
{
Calendar c=Calendar.getInstance();
String []st=new String[n];
DateFormat df=new SimpleDateFormat(dateFormatStr);
for(int i=n;i<0;i++)
{
c.setTime(date);
c.add(Calendar.DATE,i);
Date d=c.getTime();
String s=df.format(d);
st[i]=s;
}
return st;
}
/**
* <p>返回指定格式的第n天后的时间串集合</p>
* @param date
* @param n
* @param dateFormatStr
* @return n天后的时间串
*/
public static String[] getAfterNDay(int n,String dateFormatStr)
{
Calendar c=Calendar.getInstance();
String []st=new String[n];
Date date=new Date();
DateFormat df=new SimpleDateFormat(dateFormatStr);
String ss=df.format(date);
c.setTime(date);
c.add(Calendar.DATE,n);
Date d=c.getTime();
String s=df.format(d);
System.out.println(ss);
System.out.println(s);
st[0]=ss;
st[0]=s;
return st;
}
/**
* <p>根据指定格式获得时间串</p>
* @param dateString
*/
public static String getFormatDate(String inputTimeStr,String formatStr)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
@SuppressWarnings("unused")
Date date = null;
try
{
date = sdf.parse(inputTimeStr);
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat bartDateFormat = new SimpleDateFormat(formatStr);
return bartDateFormat.format(date);
}
/**
* <p>根据指定格式获得当前时间串</p>
* @param dateString
*/
public static String getFormatCurrentDate(String formatStr)
{
Date date = new Date();
SimpleDateFormat bartDateFormat = new SimpleDateFormat(formatStr);
return bartDateFormat.format(date);
}
/**
* <p>将短时间格式字符串转换为时间formatStr </p>
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate,String formatStr)
{
SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
Date strtodate = null;
try
{
strtodate = formatter.parse(strDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
return strtodate;
}
public static void main(String[] args)
{
// String[]st=getWeekOfDayEnlish("MM月dd日--EEEE");
// getWeekOfDayChina("MM月dd日");
// for(int i=0;i<st.length;i++)
// {
//
// System.out.println(st[i]);
// }
String[]st=getAfterNDay(new Date(),6,"EEEE");
for(int i=0;i<st.length;i++)
{
System.out.println(st[i]);
}
//getAfterNDay(6,"yyyy,MM月dd日");
// System.out.println(getWeeks("2009-03-12"));
// GregorianCalendar ca = new GregorianCalendar();
// System.out.println(ca.get(GregorianCalendar.AM_PM));
//
// SimpleDateFormat defaultSDF=new SimpleDateFormat("yyyyMM");
//System.out.println(strToDate("2009-3-13","yyyy-MM-dd"));
}
}