Found a very full Java processing time Date type of class

Source: Internet
Author: User
Tags string format
Also collects a good Java time format encyclopedia, this is made of the class

Java.util. *;
Import Java.text. * ;
Import Java.util.Calendar;

public class Vedate {
/**
* Get now time
*
* @return return Time type YYYY-MM-DD HH:MM:SS
*/
public static Date Getnowdate () {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String datestring = Formatter.format (currenttime);
Parseposition pos = new parseposition (8);
Date currenttime_2 = Formatter.parse (datestring, POS);
return currenttime_2;
}

/**
* Get now time
*
* @return return short time format YYYY-MM-DD
*/
public static Date Getnowdateshort () {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");
String datestring = Formatter.format (currenttime);
Parseposition pos = new parseposition (8);
Date currenttime_2 = Formatter.parse (datestring, POS);
return currenttime_2;
}

/**
* Get now time
*
* @return return string format yyyy-mm-dd HH:mm:ss
*/
public static String Getstringdate () {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String datestring = Formatter.format (currenttime);
return datestring;
}

/**
* Get now time
*
* @return Returns a short time string format YYYY-MM-DD
*/
public static String Getstringdateshort () {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");
String datestring = Formatter.format (currenttime);
return datestring;
}

/**
* Get time Hour: minute; SEC HH:mm:ss
*
* @return
*/
public static String Gettimeshort () {
SimpleDateFormat formatter = new SimpleDateFormat ("HH:mm:ss");
Date currenttime = new Date ();
String datestring = Formatter.format (currenttime);
return datestring;
}

/**
* Converts a long time format string to a time yyyy-mm-dd HH:mm:ss
*
* @param strdate
* @return
*/
public static Date Strtodatelong (String strdate) {
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Parseposition pos = new parseposition (0);
Date strtodate = Formatter.parse (strdate, POS);
return strtodate;
}

/**
* Converts a long time format time to a string yyyy-mm-dd HH:mm:ss
*
* @param datedate
* @return
*/
public static String Datetostrlong (Java.util.Date datedate) {
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String datestring = Formatter.format (datedate);
return datestring;
}

/**
* Convert short time format time to string YYYY-MM-DD
*
* @param datedate
* @param k
* @return
*/
public static String Datetostr (Java.util.Date datedate) {
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");
String datestring = Formatter.format (datedate);
return datestring;
}

/**
* Converts a short time format string to a time yyyy-mm-dd
*
* @param strdate
* @return
*/
public static Date strtodate (String strdate) {
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");
Parseposition pos = new parseposition (0);
Date strtodate = Formatter.parse (strdate, POS);
return strtodate;
}

/**
* Get the time now
*
* @return
*/
public static Date Getnow () {
Date currenttime = new Date ();
return currenttime;
}

/**
* Extract the last day of one months
*
* @param Day
* @return
*/
public static Date Getlastdate (Long day) {
Date date = new Date ();
Long date_3_hm = Date.gettime ()-3600000 * * day;
Date date_3_hm_date = new Date (DATE_3_HM);
return date_3_hm_date;
}

/**
* Get the time now
*
* @return String YyyyMMdd HHMMSS
*/
public static String Getstringtoday () {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("YyyyMMdd hhmmss");
String datestring = Formatter.format (currenttime);
return datestring;
}

/**
* Get the hour Now
*/
public static String Gethour () {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String datestring = Formatter.format (currenttime);
String hour;
hour = datestring.substring (11, 13);
Return hour;
}

/**
* Get the minute now
*
* @return
*/
public static String GetTime () {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String datestring = Formatter.format (currenttime);
String min;
min = datestring.substring (14, 16);
return min;
}

/**
* According to the time the user passed in the format, return the current time format if it is YYYYMMDD, note that the letter Y can not capitalize.
*
* @param Sformat
* YYYYMMDDHHMMSS
* @return
*/
public static string Getuserdate (String sformat) {
Date currenttime = new Date ();
SimpleDateFormat formatter = new SimpleDateFormat (Sformat);
String datestring = Formatter.format (currenttime);
return datestring;
}

/**
* Two hours between the difference, you must ensure that two times are "hh:mm" format, return character of the minute
*/
public static string Gettwohour (String st1, String st2) {
string[] KK = null;
string[] JJ = null;
KK = St1.split (":");
JJ = St2.split (":");
if (Integer.parseint (kk[0]) < Integer.parseint (jj[0]))
return "0";
else {
Double y = double.parsedouble (kk[0]) + double.parsedouble (kk[1])/60;
Double u = double.parsedouble (jj[0]) + double.parsedouble (jj[1])/60;
if ((y-u) > 0)
return y-u + "";
Else
Return "0
Related Article

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.