⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysuperdate.java

📁 模拟的土豆网视频网站
💻 JAVA
字号:
package cn.myvideosite.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Pattern;

public class MySuperDate {
     private Date myDate;
     private static final String patternDate="yyyy-MM-dd";
     private static final String patternYearMonth="yyyy-MM";
     private static final String patternDateTime="yyyy-MM-dd HH:mm:ss";
     private static final String patternDateMinutes="yyyy-MM-dd HH:mm";
     private static final String patternDateTimeString="yyyyMMddHHmmss";
     private SimpleDateFormat sdf;
     private GregorianCalendar cal;
     
     /**
      * 初始化日期
      */
     private void initialize(){
    	 cal=new GregorianCalendar();
    	 cal.setTime(this.myDate);
     }
     /**
      *一系列构造方法
      */
     public MySuperDate(){
    	 this.myDate=new Date();
    	 initialize();
     }
     public MySuperDate(long dlong){
    	 this.myDate=new Date();
    	 this.myDate.setTime(dlong);
    	 initialize();
     }
     public MySuperDate(Date ddate){
    	 this.myDate=ddate;   	 
    	 initialize();
     }
     
     /*public MySuperDate(String dateTimeString){
    	 //this(dateTimeString, true);
    	 
     }*/

	public MySuperDate(String dateTimeString, boolean isDateTime){
    	 if(dateTimeString==null|| dateTimeString.length()==0){
    		 this.myDate=new Date();
    	 }else{
    		 if(isDateTime) sdf=new SimpleDateFormat(patternDateTime);
    		 else sdf=new SimpleDateFormat(patternDate);
    		 this.myDate=sdf.parse(dateTimeString, new ParsePosition(0));
    	 }
    	 initialize();
     }
	 public MySuperDate(String dateMinutes){
		 if(dateMinutes ==null || dateMinutes.length() == 0){
			 this.myDate=new Date();
		 }else{
			 sdf=new SimpleDateFormat(patternDateMinutes); 
			 try {
				this.myDate=sdf.parse(dateMinutes);
			} catch (ParseException e) {			
				e.printStackTrace();
			}
		 }
		 initialize();		
	 }
     public MySuperDate(String Year,String Month,String Day){
    	 sdf=new SimpleDateFormat(patternDate);
    	 this.myDate=sdf.parse(Year+"-"+Month+"-"+Day,new ParsePosition(0));
    	 initialize();
     }
     public MySuperDate(int Year,int Month,int Day){
    	 sdf=new SimpleDateFormat(patternDate);
    	 this.myDate=sdf.parse(Integer.toString(Year)+"-"+Integer.toString(Month)+"-"+
    			 Integer.toString(Day),new ParsePosition(0));
    	 initialize();
     }
     public MySuperDate(String Year,String Month,String Day,String Hour,String Minute,String Second){
    	 sdf=new SimpleDateFormat(patternDateTime);
    	 this.myDate=sdf.parse(Year+"-"+Month+"-"+Day+"  "+Hour+":"+Month+":"+Second,new ParsePosition(0));
    	 initialize();
     }
     public MySuperDate(int Year,int Month,int Day,int Hour,int Minute,int Second){
    	 sdf=new SimpleDateFormat(patternDate);
    	 this.myDate=sdf.parse(Integer.toString(Year)+"-"+Integer.toString(Month)+"-"+
    			 Integer.toString(Day)+" "+Integer.toString(Hour)+":"+Integer.toString(Minute)
    			 +":"+Integer.toString(Second),new ParsePosition(0));
    	 initialize();
     }
     /**
      * get方法
      * @return
      */
     public int getYear(){
    	 return cal.get(Calendar.YEAR);
     }
     public int getMonth(){
    	 return cal.get(Calendar.MONTH)+1;
     }
     public int getDay(){
    	 return cal.get(Calendar.DAY_OF_MONTH);
     }
     public int getHour(){
    	 return cal.get(Calendar.HOUR_OF_DAY);
     }
     public int getMinute(){
    	 return cal.get(Calendar.MINUTE);
     }
     public int getSecond(){
    	 return cal.get(Calendar.SECOND);
     }
     public long getMilliSecond(){
    	 return cal.getTime().getTime();
     }
     public boolean after(MySuperDate when){
    	 return this.myDate.after(when.myDate);
     }
     public boolean before(MySuperDate when){
    	 return this.myDate.before(when.myDate);
     }
     public int compareTo(MySuperDate when){
    	 return this.myDate.compareTo(when.myDate);
     }
     public String getDateString(){
    	 sdf=new SimpleDateFormat(patternDate);
    	 return (sdf.format(this.myDate));
     }
     public String getDateTimeString(){
    	 sdf=new SimpleDateFormat(patternDateTime);
    	 return (sdf.format(this.myDate));
     }
     public String getDateTimeStrString(){
    	 sdf=new SimpleDateFormat(patternDateTimeString);
    	 return (sdf.format(this.myDate));
     }
     public String getYearMonthString(){
    	 sdf=new SimpleDateFormat(patternYearMonth);
    	 return (sdf.format(this.myDate));
     }
     public static String formatDateTime(Date d,String pattern){
    	 SimpleDateFormat sdf2=new SimpleDateFormat(pattern,Locale.CANADA);
    	 return  (sdf2.format(d));
     }
     public String getFormatDateTime(String pattern){
    	 return new SimpleDateFormat(pattern).format(myDate);
     }
     public Date getDate(){
    	 return this.myDate;
     }
     public Calendar getCalendar(){
    	 return this.cal;
     }
     public void add(int field,int amount){
    	this.cal.add(field, amount);
    	this.myDate=cal.getTime();
     }
     public MySuperDate getAddedDate(int field,int amount){
    	 MySuperDate addedSuperDate=new MySuperDate(this.myDate);
    	 addedSuperDate.add(field, amount);
    	 return addedSuperDate;
     }
     public static MySuperDate getFirstDayInMonthDate(MySuperDate thesdt){
    	 return new MySuperDate(thesdt.getYear()+"-"+thesdt.getMonth()+"-01 00:00:00");
     }
     public static boolean isToday(MySuperDate sdf3,MySuperDate sdf4){
    	 return !sdf3.before(new MySuperDate(sdf4.getDateString()+"00:00:00"))&&
    	        !sdf3.after(new MySuperDate(sdf4.getDateString()+"23:59:59"));
     }
     public static long getTimeInteval(MySuperDate sdf5,MySuperDate sdf6,int intUnit){
    	 long intevalms;
    	 long returnValue=0;
    	 intevalms=sdf6.myDate.getTime()-sdf5.myDate.getTime();
    	 switch(intUnit){
    	      case Calendar.DAY_OF_MONTH:
    	            returnValue=intevalms/(1000*3600*24);
    	            break;
    	      case Calendar.DAY_OF_YEAR:
	            returnValue=intevalms/(1000*3600*24);
	            break;   
    	      case Calendar.HOUR:
    	    	  returnValue=intevalms/(1000*3600);
    	      case Calendar.MINUTE:
    	    	  returnValue=intevalms/(1000*60); 
    	      case Calendar.SECOND:
    	    	  returnValue=intevalms/1000; 
    	 }
    	 return (returnValue);
     }
     /**
      * 取得这个月的最大日
      */
     public int getActualMaximumDaysInMonth(){
    	 return getCalendar().getActualMaximum(Calendar.DAY_OF_MONTH);
     }
     public static String getPatternDateMinutes() {
 		return patternDateMinutes;
 	}
     /**
      * 取得这个月的第一个星期的第一天
      */
     public int getActualFirstWeekDayInMonth(){
    	 Calendar calendar=new MySuperDate(getDate()).getCalendar();
    	 calendar.set(Calendar.DAY_OF_MONTH, 1);
    	 return calendar.get(Calendar.DAY_OF_WEEK);
     }
     public String getPrevSunday(MySuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, -1*(7+week-1)).getDateString();
     }
     public String getNextSunday(MySuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, (7+week-1)).getDateString();
     }
     public String getNextThirdSunday(MySuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, (14-week+1)).getDateString();
     }
     public String getCurrSunday(MySuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, (1-week)).getDateString(); 
     }
     public String getCurrSaturDay(MySuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, (7-week)).getDateString();
     }
     public Date getTodayStartTime(){
    	 this.cal.set(this.getYear(), this.getMonth()-1,this.getDay(),0,0,0);
    	 return this.cal.getTime();
     }
     public Date getTodayNextTime(){
    	 this.cal.set(this.getYear(), this.getMonth()-1,this.getDay(),0,0,0);
    	 this.cal.add(GregorianCalendar.DAY_OF_YEAR, 1);
    	 return this.cal.getTime();
     }
     public String getEventDateString(){
    	 String strYear=String.valueOf(getYear()).substring(2);
    	 String strMonth=String.valueOf(getMonth());
    	 if(getMonth()<10)strMonth="0"+strMonth;
    	 String strDay=String.valueOf(getDay());
    	 if(getDay()<10)strDay="0"+strDay;
    	 return strYear+"."+strMonth+"."+strDay;
     }
     
     public static MySuperDate getSuperDateNotNull(Date ld,String def_date){
    	 MySuperDate sdtemp=null;
    	 try{
    	       sdtemp=new MySuperDate(ld);
    	 }catch(Exception e){
    		 if(!isDateString(def_date)) sdtemp=new MySuperDate();
    		 else sdtemp=new MySuperDate(def_date);
    	 }
    	 return sdtemp;
     }
     public static boolean isDateTimeString(String s) {
 		
 		return match("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})", s);
 	}
      
	public  static boolean isDateString(String s) {
		
		return match("(\\d{4})-(\\d{2})-(\\d{2})", s);
	}
	public static boolean match(String regex,String str){
		return Pattern.matches(regex, str);
	}
	public int getDiscrepancyDay(){
		long dis=new Date().getTime()-this.myDate.getTime();
		return (int )(dis/(24*60*60*1000));
	}
	public static int getAge(Date birthday){
		return new MySuperDate().getYear()-new MySuperDate(birthday).getYear();
	}
	public static void main(String[] args){
		MySuperDate sdt=new MySuperDate("1988-02-15 11:11:11");
		System.out.println(MySuperDate.getAge(sdt.getDate()));
		MySuperDate sdt2=new MySuperDate("1988-02-15 11:11:11");
		MySuperDate sdt3=getFirstDayInMonthDate(sdt2);
		System.out.println(sdt3);
		Date d=new MySuperDate().getTodayNextTime();
		MySuperDate sdt4=new MySuperDate(d);		
		System.out.println(sdt4.getDateTimeString());
	}
	
     
     
     
     
     
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -