Conversion of the tool class time format for Java date operations

Source: Internet
Author: User
Tags date now

Package cn.itcast.oa.util;

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

Import javax.xml.datatype.DatatypeConfigurationException;
Import Javax.xml.datatype.DatatypeFactory;
Import Javax.xml.datatype.XMLGregorianCalendar;

public class Dateutils {
/**
* Converts the specified string to a date
*
* @param date
* String Date strings
* @param datepattern
* String Date format
* @return Date
*/
public static Java.util.Date Getformatdate (String Date, String datepattern) {
SimpleDateFormat sd = new SimpleDateFormat (Datepattern);
return Sd.parse (date, new Java.text.ParsePosition (0));
}


/**
* Converts a specified Date object to a formatted string
*
* @param date
* Date XML Dates Object
* @param datepattern
* String Date format
* @return String
*/
public static string getformattedstring (date date, string datepattern) {
SimpleDateFormat sd = new SimpleDateFormat (Datepattern);

return Sd.format (date);
}

/**
* Converts the specified XML Date object into a formatted string
*
* @param xmldate
* Date XML Dates Object
* @param datepattern
* String Date format
* @return String
*/
public static String getformattedstring (Xmlgregoriancalendar xmldate,
String Datepattern) {
SimpleDateFormat sd = new SimpleDateFormat (Datepattern);

Calendar calendar = Xmldate.togregoriancalendar ();

Return Sd.format (Calendar.gettime ());
}

/**
* Converts the specified XML Date object into a Date object
*
* @param xmldate
* Date XML Dates Object
* @param datepattern
* String Date format
* @return Date
*/
public static Date xmlgregoriancalendar2date (Xmlgregoriancalendar xmldate) {
Return Xmldate.togregoriancalendar (). GetTime ();
}

public static String Getthisyear () {
Get Current date
Calendar cldcurrent = Calendar.getinstance ();
Acquired date
String stryear = string.valueof (Cldcurrent.get (calendar.year));
return stryear;
}

public static Xmlgregoriancalendar Convert2xmlcalendar (Calendar calendar) {
try {
Datatypefactory DTF = Datatypefactory.newinstance ();
Return Dtf.newxmlgregoriancalendar (
Calendar.get (Calendar.year),
Calendar.get (calendar.month) +1,
Calendar.get (Calendar.day_of_month),
Calendar.get (Calendar.hour),
Calendar.get (Calendar.minute),
Calendar.get (Calendar.second),
Calendar.get (Calendar.millisecond),
Calendar.get (Calendar.zone_offset)/(1000*60));

} catch (Datatypeconfigurationexception e) {
E.printstacktrace ();
return null;
}
}

Get the time of day
public static Java.sql.Timestamp Getnowtime (String dateformat) {
Date now = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat (DateFormat);//You can easily modify the date format
String datestring = Dateformat.format (now);
SimpleDateFormat sd = new SimpleDateFormat (DateFormat);
Date dateformt = Sd.parse (datestring, New java.text.ParsePosition (0));
Java.sql.Timestamp dateTime = new Java.sql.Timestamp (dateformt
. GetTime ());

return dateTime;
return hehe;
}

Gets the specified time
public static Java.sql.Timestamp Getnownewtime (String date,string dateformat) {
Date now = new Date ();
SimpleDateFormat DateFormat = new SimpleDateFormat (DateFormat);//You can easily modify the date format
Dateformat.parse (date, new Java.text.ParsePosition (0));

String datestring= Dateformat.format (date);
Date dateformt= dateformat.parse (date, new Java.text.ParsePosition (0));
Java.sql.Timestamp dateTime = new Java.sql.Timestamp (Dateformt.gettime ());


return dateTime;
return hehe;
}
/**
* @param contains yyyy-mm-dd ' T ' hh:mm:ss. Time conversion in SSS format.
* @return
*/
public static string Gettformatstring (String tdate) {
SimpleDateFormat format1 = new SimpleDateFormat ("Yyyy-mm-dd ' T ' HH:mm:ss. SSS ");
String str = "";
try {
Java.util.Date Date = Format1.parse (tdate);
SimpleDateFormat format2 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
str = Format2.format (date);
} catch (ParseException e) {
E.printstacktrace ();
}
return str;
}

public static void Main (string[] args) {
SimpleDateFormat sd = new SimpleDateFormat ("YyyyMMdd");
String date = "20110202";
System.out.println (Sd.parse (date, new Java.text.ParsePosition (0)));
System.out.println (Getbefore2hourdate ());
}
Gets the time 2 hours before the current time.
public static String Getbefore2hourdate () {
SimpleDateFormat df=new SimpleDateFormat ("Yyyy-mm-dd ' T ' HH:mm:ss");
Calendar C = calendar.getinstance ();
C.add (Calendar.hour_of_day,-2); The current time plus 3 hours
Return Df.format (C.gettime ());

}

/**
*
* @param time1 Current time
* @param time2 comparison time
* @return If time1 is larger than time2 minutes, it returns true;
*/
public static Boolean comparedatetime (date time1, date time2, int gap) {
Return Time1.gettime ()-time2.gettime () > Gap * 60 * 1000;
}
}

Conversion of the tool class time format for Java date operations

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.