java time date usage and query code detail

  • 2020-12-05 17:13:22
  • OfStack

As long as the format is correct, direct comparison of strings can be ah, accurate to the second is also 1 kind


String  s1  =  "2003-12-12  11:30:24";  
 String  s2  =  "2004-04-01  13:31:40";  
 int  res  =  s1.compareTo(s2);  

O date difference


SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;// Divided by the 1000 In order to convert it into seconds   
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 

Time and date classes are often used, so the commonly used date methods and properties are summarized as follows, easy to find

1. Calculate the maximum number of days in a January


Calendar time=Calendar.getInstance(); 
time.clear(); 
time.set(Calendar.YEAR,year); 
time.set(Calendar.MONTH,i-1);// Pay attention to ,Calendar The objects 1 Month for 0 
int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);// Days of the month  

Note: Before using the set method, it is best to use clear1 first, otherwise much of the information will inherit from the current system time

2. Transformation of Calendar and Date

(1) Conversion from Calendar to Date


Calendar cal=Calendar.getInstance(); 
Date date=cal.getTime(); 

(2) Convert Date to Calendar


Date date=new Date(); 
Calendar cal=Calendar.getInstance(); 
cal.setTime(date); 

3. Format the output date and time


Date date=new Date(); 
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
System.out.println(df.format(date)); 

4. Count the week of the year

(1) Calculate the week of the year for a given day


Calendar cal=Calendar.getInstance(); 
cal.set(Calendar.YEAR, 2006); 
cal.set(Calendar.MONTH, 9); 
cal.set(Calendar.DAY_OF_MONTH, 3); 
int weekno=cal.get(Calendar.WEEK_OF_YEAR); 

(2) Calculate the day of the week of the year


SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
Calendar cal=Calendar.getInstance(); 
cal.set(Calendar.YEAR, 2006); 
cal.set(Calendar.WEEK_OF_YEAR, 1); 
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
System.out.println(df.format(cal.getTime())); 

Output:


2006-01-02 

5. Use of add() and roll()
(1) add () method


SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
Calendar cal=Calendar.getInstance(); 
cal.set(Calendar.YEAR, 2006); 
cal.set(Calendar.MONTH, 9); 
cal.set(Calendar.DAY_OF_MONTH, 3); 
cal.add(Calendar.DATE, -4); 
Date date=cal.getTime(); 
System.out.println(df.format(date)); 
cal.add(Calendar.DATE, 4); 
date=cal.getTime(); 
System.out.println(df.format(date)); 

Output:
2006-08-30
2006-10-03

(2) roll method


SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;// Divided by the 1000 In order to convert it into seconds   
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 
0

Output:

2006-10-29

2006-10-03

It can be seen that the roll() method circulates in the month, 1 generally uses the add() method;

6. Calculate the number of days between two arbitrary times

(1) Pass into the Calendar object


SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;// Divided by the 1000 In order to convert it into seconds   
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 
1

(2) Pass into the Date object


SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;// Divided by the 1000 In order to convert it into seconds   
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 
2

Similarly, the number of hours, minutes, seconds, etc. separated by any two times can be calculated in the same way
Note: The above method is calculated entirely on a time basis and is sometimes unsatisfactory. For example:


startday="2006-10-11 20:00:00" 
endday="2006-10-12 8:00:00" 

The calculated result is 0, but we may change the calculated result to 1, which can be achieved by the following method:
Before passing the reference, set the time of endday, such as:


SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;// Divided by the 1000 In order to convert it into seconds   
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 
4

So pass it on to startday,endday, and we get what we want. However, if the above methods are troublesome, you can refer to the following methods:
(3) Improve the method for accurately calculating the number of days apart


SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;// Divided by the 1000 In order to convert it into seconds   
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 
5

Get the current time of the system:


SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;// Divided by the 1000 In order to convert it into seconds   
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 
6

In developing the web application, we need to do various conversions to date types in our program for different database date types. If the corresponding database data is the Date type of oracle, that is, only the year, month and day, you can choose to use the ES109en.sql.Date type; if the corresponding database is the DateTime type of MSsqlserver database, that is, the minute and second of the year, month and day, select the ES114en.sql.Timestamp type

You can use dateFormat to define the time and date format by turning 1 string


SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;// Divided by the 1000 In order to convert it into seconds   
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 
7

Here are some examples:

Java code


package test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import javax.swing.JOptionPane;
public class Test2{
	public static Boolean isdate(String s){
		String a[]=s.split("-");
		Boolean flg=true;
		if(!(Integer.parseint(a[0])>=1950 && Integer.parseint(a[0])<=2050)){
			flg=false;
		}
		return flg;
	}
	public static Boolean checkDate(String s){
		Boolean ret = true;
		try{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			ret = df.format(df.parse(s)).equals(s);
		}
		catch(ParseException e){
			ret = false;
		}
		return ret;
	}
	public Object dateinfo(String s){
		String a[]=s.split("-",2);
		Hashtable fest =new Hashtable();
		fest.put("01-01"," On New Year's day ");
		fest.put("02-14"," Valentine's Day ");
		fest.put("03-12"," Arbor Day ");
		fest.put("03-15"," Consumer's day ");
		fest.put("04-01"," April Fools' Day ");
		fest.put("04-05"," Qingming Festival ");
		fest.put("05-01"," International Workers' Day ");
		fest.put("06-01"," Children's day ");
		fest.put("07-01"," The founding section ");
		fest.put("08-01"," The army day ");
		fest.put("09-10"," Teacher's day ");
		fest.put("10-01"," The National Day ");
		fest.put("12-25"," Christmas ");
		if(fest.containsKey(a[1])){
			return fest.get(a[1]);
		} else{
			return " There is no holiday ";
		}
	}
	public String xingzuo(Date s){
		Calendar cal = Calendar.getInstance();
		cal.setTime(s);
		String xingzuo=" There is no ";
		int day=cal.get(Calendar.DAY_OF_YEAR);
		if((cal.get(Calendar.YEAR)%4==0)&&(cal.get(Calendar.YEAR)%100!=0)||(cal.get(Calendar.YEAR)%400==0)){
			if((day>=1 &&day<=19)||(day>=357&&day<=366)){
				xingzuo= " The magic Capricorn ";
			} else if(day>=20 &&day<=49){
				xingzuo= " Aquarius: ";
			} else if(day>=50 &&day<=80){
				xingzuo= " Pisces ";
			} else if(day>=81 &&day<=110){
				xingzuo= " Aries ";
			} else if(day>=111 &&day<=141){
				xingzuo= " Taurus ";
			} else if(day>=142 &&day<=173){
				xingzuo= " Gemini ";
			} else if(day>=174 &&day<=203){
				xingzuo= " cancer ";
			} else if(day>=204 &&day<=235){
				xingzuo= " Leo ";
			} else if(day>=236 &&day<=266){
				xingzuo= " virgo ";
			} else if(day>=267 &&day<=296){
				xingzuo= " libra ";
			} else if(day>=297 &&day<=326){
				xingzuo= " Scorpio ";
			} else if(day>=327 &&day<=356){
				xingzuo= " Sagittarius ";
			}
		} else{
			if((day>=1 &&day<=19)||(day>=357&&day<=366)){
				xingzuo= " The magic Capricorn ";
			} else if(day>=20 &&day<=48){
				xingzuo= " Aquarius: ";
			} else if(day>=49 &&day<=79){
				xingzuo= " Pisces ";
			} else if(day>=80 &&day<=109){
				xingzuo= " Aries ";
			} else if(day>=110 &&day<=140){
				xingzuo= " Taurus ";
			} else if(day>=141 &&day<=172){
				xingzuo= " Gemini ";
			} else if(day>=173 &&day<=202){
				xingzuo= " cancer ";
			} else if(day>=203 &&day<=234){
				xingzuo= " Leo ";
			} else if(day>=235 &&day<=265){
				xingzuo= " virgo ";
			} else if(day>=266 &&day<=295){
				xingzuo= " libra ";
			} else if(day>=296 &&day<=325){
				xingzuo= " Scorpio ";
			} else if(day>=326 &&day<=355){
				xingzuo= " Sagittarius ";
			}
		}
		return xingzuo;
	}
	public Date parseDate(String s){
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date3 = bartDateFormat.parse(s);
			date3=bartDateFormat.parse(s);
			return date3;
		}
		catch (Exception ex) {
			return null;
		}
	}
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		Test2 test2=new Test2();
		String date1=JOptionPane.showInputDialog(" Please enter the date in such format 2000-10-15");
		while(!(Test2.checkDate(date1)&&Test2.isdate(date1))){
			date1=JOptionPane.showInputDialog(" Please enter the date in such format 2000-10-15");
		}
		SimpleDateFormat bartDateFormat1 = new SimpleDateFormat("yyyy,MM,dd,EEEE");
		SimpleDateFormat bartDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
		Date date2=test2.parseDate(date1);
		String festinfo=(String)test2.dateinfo(date1);
		System.out.println(bartDateFormat1.format(date2) +","+ festinfo+","+ test2.xingzuo(date2));
		String day=JOptionPane.showInputDialog(" Please enter your query N Date information of days ");
		cal.setTime(date2);
		cal.add(Calendar.DATE, Integer.parseint(day));
		String date5=bartDateFormat2.format(cal.getTime());
		festinfo=(String)test2.dateinfo(date5);
		System.out.println(bartDateFormat1.format(cal.getTime())+","+ festinfo+","+ test2.xingzuo(cal.getTime()));
	}
}

conclusion

That is the end of this article on java time and date usage and query code detail, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: