Date time Formatting (Java)

Source: Internet
Author: User

Package com.lvxh.service;

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

Import Org.apache.commons.lang3.StringUtils;
Import Org.apache.log4j.Logger;

/**
* . Date Tool 2015-09-10
*
* @since V100R001C02
* @version V100R001C02
*/
Public abstract class Dateutil {

/**
* Initialize Log class
*/
private static final Logger LOG = Logger.getlogger (Dateutil.class);

/**
*. date format
*/
private static final String Date_format = "YYYY-MM-DD";

/**
*. date format
*/
public static final String Datetime_format = "Yyyy-mm-dd HH:mm:ss";

/**
*. date format
*/
private static final int millisencond_of_minute = 60 * 1000;

/**
* . Date Copy
*
* @param date
* Source Date Object
* @return The Date object after copying
*/
public static date Clone (date date) {
if (date! = null) {
Return (Date) Date.clone ();
}
return null;
}

/**
* Time type go to string
*
* @return String
*/
public static String Gettoday () {

Return Datetostr (New Date ());
}

/**
* Time type go to string
*
* @param date
* Date
*
* @return String
*/
public static String datetostr (date date) {
return Datetostr (date, date_format);
}

/**
* Time type go to string
*
* @param date
* Date
*
* @param format
* Format
*
* @return String
*/
public static string Datetostr (date date, string format) {

if (date = = null) {
Return "";
} else {
SimpleDateFormat SDF = new SimpleDateFormat (format);
return Sdf.format (date);
}
}

/**
* Time string converted to date
*
* @param date
* string to be converted by string
* @param format
* Conversion Format
* @return Date returned
*/
public static date Strtodate (string date, string format) {
Date time = null;
if (Stringutils.isempty (date)) {
Log.info ("date is null");
} else {
SimpleDateFormat SDF = new SimpleDateFormat (format);
try {
Time = Sdf.parse (date);
} catch (ParseException e) {
Log.error ("Parse Time exception", e);
}
}
return time;
}

/**
* Function Description: Time subtraction to get the number of minutes
*
* @param begindate
* Begindate
* @param endDate
* EndDate
* @return Long
*/
public static long subminutes (date begindate, date endDate) {
Return (Enddate.gettime ()-begindate.gettime ())/Millisencond_of_minute;
}
}

Date time Formatting (Java)

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.