If I date1="1987-01-01"
date2="2010-01-01"
How many days are there? ?
How to calculate in java. . . . . . ? ? ?
Java can use to calculate the difference in the number of days on a date, and the following is the detailed code:
import ;
import ;
import ;
import ;
public class test16 {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1=("2012-09-08 10:10:10");
Date d2=("2012-09-15 00:00:00");
(daysBetween(d1,d2));
(daysBetween("2012-09-08 10:10:10","2012-09-15 00:00:00"));
}
/**
* Calculate the number of days that differ between two dates
* @param smdate Smaller time
* @param bdate Larger time
* @return Number of days
* @throws ParseException
*/
public static int daysBetween(Date smdate,Date bdate) throws ParseException
{
Calendar cal = ();
(smdate);
long time1 = ();
(bdate);
long time2 = ();
long between_days=(time2-time1)/(1000*3600*24);
return ((between_days));
}
/**
* Calculation of date format of string
*/
public static int daysBetween(String smdate,String bdate) throws ParseException{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = ();
((smdate));
long time1 = ();
((bdate));
long time2 = ();
long between_days=(time2-time1)/(1000*3600*24);
return ((between_days));
}
}
Java performs addition and subtraction operations on the date Date class, year addition and subtraction months addition and time difference, etc.
Java performs addition and subtraction operations on the date Date class, year addition and subtraction, month addition and subtraction
1. Java calculates the time difference and compares the time size (time point)
For example: It is now 2004-03-26 13:31:40
The past was: 2004-01-02 11:30:24
I now want to get two date differences, the difference is in the form: XX days XX hours XX minutes XX seconds
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
begin=("2004-01-02 11:30:24");
end = ("2004-03-26 13:31:40");
long between=(()-())/1000;//Divid by 1000 is to convert to seconds
long day1=between/(24*3600);
long hour1=between%(24*3600)/3600;
long minute1=between%3600/60;
long second1=between%60/60;
("+day1+"day"+hour1+"hour"+minute1+"minute1+"second1+"second");
Simple usage of joda dateTime
Java accurately calculates decimals
java_timestamp and Date_convert each other
1. Definition of timestamp
Timestamp refers to the creation, modification, and access time in file attributes.
Digital timestamp technology is an application of a variant of digital signature technology. In e-commerce transaction documents, time is very important information. In written contracts, the date of the document signing is as important as the signature of the document to prevent the document from being forged and tampered with. Digital Time Stamp Service (DTS: digital time stamp service) is one of the online e-commerce security services projects, which can provide security protection for date and time information of electronic files.
Edit the components of this section
A time-stamp is an encrypted credential document that consists of three parts:
(1) Summary of the file that needs to be timestamped (digest);
(2) The date and time of the file received by DTS;
(3) Digital signature of DTS.
Generally speaking, the process of generating timestamps is: the user first encrypts the file that needs to be timestamped into a digest with Hash encoding, and then sends the digest to DTS. After adding the date and time information of the file digest received, the DTS encrypts the file (digital signature), and then sends it back to the user.
The time for signing the document in writing is written by the signer himself, while the number timestamp is not the case. It is added by the certification unit DTS and is based on the time when the document is received by the DTS.
2. Convert timestamps to Date (or String)
//The timestamp is converted to Sting or Date
SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long time=newLong(445555555);
String d = (time);
Date date=(d);
("Format To String(Date):"+d);
("Format To Date:"+date);
Operation results:
Format To String(Date):1970-01-06 11:45:55
Format To Date:Tue Jan 06 11:45:55 CST 1970
3. Convert Date (or String) to timestamp]
//Date or String is converted to timestamp
SimpleDateFormat format = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time="1970-01-06 11:45:55";
Date date = (time);
("Format To times:"+());
Running results:
Format To times:445555000
4. Pay attention
When defining SimpleDateFormat, newSimpleDateFormat("yyyy-MM-dd HH:mm:ss"); there cannot be spaces at the beginning and end of the string. If there are spaces, the corresponding time format when using the conversion must also have spaces (the two are corresponding), such as:
//Date or String is converted to timestamp
SimpleDateFormat format = newSimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
String time="1970-01-06 11:45:55";
Date date = (time);
("Format To times:"+());
Running result (Error):
Exception in thread "main": Unparseable date: "1970-01-06 11:45:55"
correct:
// Convert Date or String into timestamp
SimpleDateFormat format = newSimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
String time=" 1970-01-06 11:45:55 ";//Note: After correction, spaces were added here before and after
Date date = (time);
("Format To times:"+());
Running results:
Format To times:445555000
1. GetTime() in the Date class in java obtains the timestamp. The timestamp generated in java is accurate to the millisecond level, while in unix is accurate to the second level, so the timestamp generated through java needs to be divided by 1000.
2. The following is the java code
import ;
import ;
import ;
public class Baidu {
/**
* @param args
*/
public static void main(String[] args) {
try {
String time = "2011/07/29 14:50:11";
Date date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(time);
long unixTimestamp = ()/1000;
(unixTimestamp);
} catch (ParseException e) {
();
}
}
}
JAVA date addition and subtraction operation
The mutual conversion of Date and String in java
What is the difference between() and new Date() in java?
The difference between() and new Date() in java is as follows:
() is to obtain a Calendar object and can calculate time and specify the time zone
new Date() creates a date object, which is in utc format by default.
The two can be transformed into each other:
Calendar calendar = ();
// Get the Date object from a Calendar object
Date date = ();
// Reflect the Date object into a Calendar object,
// Calendar/GregorianCalendar No constructor can accept Date objects
// So we must first get an instance and then set the Date object
(date);