Time-Date conversion + Two date subtraction

Source: Internet
Author: User

Package m;

Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

public class Datet {
Date type conversion to String type
Formattype format YYYY-MM-DD hh:mm:ss//yyyy mm month dd Day HH mm min ss sec
Time of data date type
public static string datetostring (Date data, string formattype) {
return new SimpleDateFormat (formattype). Format (data);
}

A long type is converted to a string type
CurrentTime the long type of time to convert
Formattype the time format of the string type to convert
public static string longtostring (long currenttime, string formattype)
Throws ParseException {
Date date = Longtodate (CurrentTime, formattype); A long type turns into a date type
String strtime = datetostring (date, formattype); Date type turns into string
return strtime;
}

String type conversion to date type
Strtime the time of the string type to convert, formattype the format to convert YYYY-MM-DD hh:mm:ss//yyyy year MM month DD Day
hh mm min SS sec,
The time format of the strtime must be the same as the formattype time format
public static Date Stringtodate (String strtime, String formattype)
Throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat (formattype);
Date date = null;
Date = Formatter.parse (Strtime);
return date;
}

Long converts to date type
CurrentTime the long type of time to convert
Formattype the time format to convert YYYY-MM-DD hh:mm:ss//yyyy mm month dd Day HH mm min ss sec
public static Date Longtodate (Long currenttime, String formattype)
Throws ParseException {
Date Dateold = new Date (currenttime); Number of milliseconds according to long type of life a date type of time
String sdatetime = datetostring (Dateold, formattype); Convert time of date type to string
Date date = Stringtodate (Sdatetime, formattype); To convert a string type to a date type
return date;
}

String type conversion to long type
Strtime the time of the string type to convert
Formattype time Format
Strtime time format and formattype time format must be the same
public static long Stringtolong (string strtime, String formattype)
Throws ParseException {
Date date = Stringtodate (Strtime, formattype); The string type is converted to a date type
if (date = = null) {
return 0;
} else {
Long currenttime = Datetolong (date); Date type turns into long type
return currenttime;
}
}

The date type is converted to a long type
Date time of date type to convert
public static long Datetolong (date date) {
return Date.gettime ();
}

}

public static void Main (string[] args) throws Exception {
Long Startt=datet.stringtolong ("2015-03-03 14:51:23", "Yyyy-mm-dd HH:mm:ss"); Define between the machines
Long Endt=datet.stringtolong ("2014-12-03 13:50:23", "Yyyy-mm-dd HH:mm:ss"); Define the machine time
Long ss= (Startt-endt)/(1000); Total number of seconds
int MM = (int) ss/60; Total number of minutes
int hh= (int) ss/3600; Total number of hours
int dd= (int) hh/24; Total Days
SYSTEM.OUT.PRINTLN ("Total" +dd+ "days accurate time is:" +hh+ "hours" +mm+ "minutes" +ss+ "seconds Total:" +ss*1000+ "milliseconds");

}

From: http://www.blogjava.net/weishuangshuang/archive/2012/09/27/388712.html

From: http://blog.csdn.net/yuqinying112/article/details/7314278

Time-Date conversion + Two date subtraction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.