关于日期 Date Calendar_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 关于日期 Date Calendar

关于日期 Date Calendar

 2013/7/24 0:40:22  zhangIT  程序员俱乐部  我要评论(0)
  • 摘要:importjava.security.Timestamp;importjava.text.DateFormat;importjava.text.SimpleDateFormat;importjava.util.*;publicclassDate{publicstaticintgetIntervalDays(Calendarstartday,Calendarendday){//确保startday在endday之前if(startday.after(endday))
  • 标签:
class="java" name="code">
import java.security.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Date {
	
	public static int getIntervalDays(Calendar startday,Calendar endday){

        //确保startday在endday之前
		if(startday.after(endday)){               //判断
			Calendar cal=startday;
		    startday=endday;
			endday=cal;
		}
		//分别得到两个时间的毫秒数
		long sl=startday.getTimeInMillis();
		long el=endday.getTimeInMillis();
		long ei=el-sl;
		//根据毫秒数计算间隔天数
		return (int)(ei/(1000*60*60*24));
    }
	
	//提高计算时间的精度
	public int getDaysBetween (Calendar d1, Calendar d2){
		if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
			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);
		}
		return days;
}
	public static String getSystemTime(){  //好方法
		java.util.Date date21=new java.util.Date();  //一定要因对包
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("***"+df.format(date21));
		return df.format(date21.getTime());
	}
	
	//明天日期
	public static void getTomorrow(){   //很好的方法
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss G E D F w W a E F");
		java.util.Date myDate=new java.util.Date();
		long myTime=(myDate.getTime()/1000)+60*60*24;
		myDate.setTime(myTime*1000);
		String mDate=formatter.format(myDate);
		System.out.println(mDate);
	}
	
	//将字符串类型的日期转换为一个Date
	public final static java.sql.Date string2Date(String dateString)throws java.lang.Exception {
			DateFormat dateFormat;
			dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		    dateFormat.setLenient(false);
			java.util.Date timeDate = dateFormat.parse(dateString);//util类型
			java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());//sql类型
			return dateTime;
}
	public final static java.sql.Timestamp string2Time(String dateString)throws java.text.ParseException {
			DateFormat dateFormat;
			dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);//设定格式
			dateFormat.setLenient(false);
			java.util.Date timeDate = dateFormat.parse(dateString);//util类型
			java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp类型,timeDate.getTime()返回一个long型
			return dateTime;
		}




	
	public static void main(String args[]) throws Exception{
		
		Date d = new Date();
		System.out.println(d);
		//System.out.println(String.format("%tr", new Date()));
		
		//***************************************************
		Calendar calendar = Calendar.getInstance();
		System.out.println(calendar.getTime());         //获得当前时间
		
		Calendar calendar1 = Calendar.getInstance();
		java.util.Date date = calendar1.getTime();
		System.out.println(date);
		
		//***********************************************
		Date date2=new Date();
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
		System.out.println(calendar.getTime());       
		//****************************************************
		java.util.Date date3=new java.util.Date();
	    SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		System.out.println("***"+df.format(date3));
        //******************************************************
		Calendar cal3=Calendar.getInstance();
		cal3.set(Calendar.YEAR, 2006);
		cal3.set(Calendar.MONTH, 8);
		cal3.set(Calendar.DAY_OF_MONTH, 3);
		int weekno=cal3.get(Calendar.WEEK_OF_YEAR);
		System.out.println(cal3.getTime());
		
		//或者  cal.set(2004,1,6,5,40);  表示为2004年1月6号 5点40分
		
		//********************************************************
		SimpleDateFormat df4=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal4=Calendar.getInstance();
		cal4.set(Calendar.YEAR, 2006);
		cal4.set(Calendar.WEEK_OF_YEAR, 1);
		cal4.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		System.out.println(df.format(cal4.getTime()));
        //**************************************************
		SimpleDateFormat df5=new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal5=Calendar.getInstance();
		cal5.set(Calendar.YEAR, 2006);
		cal5.set(Calendar.MONTH, 8);
		cal5.set(Calendar.DAY_OF_MONTH, 3);
		cal5.add(Calendar.DATE, -4);
		java.util.Date date5=cal5.getTime(),    date6;
		System.out.println(df5.format(date5));
		cal5.add(Calendar.DATE, 4);
		date6=cal5.getTime();
		System.out.println(df5.format(date6));
//		2006-08-30
//		2006-09-03
		//*****************************************************
		System.out.println(getIntervalDays(cal4,cal5));  //计算出 两个日期之间的天数
		
		//***********************************************************
		System.out.println(getSystemTime());
		getTomorrow();
		
		
		//字符串  ——》 时间的转变
		String sToDate = "2005-8-18";//用于转换成java.sql.Date的字符串
		String sToTimestamp = "2005-8-18 14:21:12.123";//用于转换成java.sql.Timestamp的字符串
		java.util.Date date1 = string2Date(sToDate);     //
        System.out.println(date1);
        java.sql.Timestamp   date22 = string2Time(sToTimestamp);   //
        System.out.println(date22);
	}
}


  • 相关文章
发表评论
用户名: 匿名