Java Date type conversions

Source: Internet
Author: User
Tags stub
 
Import Java.sql.Timestamp;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;


Import Java.util.GregorianCalendar; 
/** * Tool class for type conversions associated with the date type * * @author vinegar * */public class Dateswitch {/** * Convert calendar type to string type required seconds * * @param Switchcalendar */public static String switchstring (Calendar cal) {//TODO auto-generated method stub Simpledat
Eformat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String datestr = Df.format (Cal.gettime ());
return datestr; /** * Convert String type to Calendar type required seconds * * @param str * @return/public static Calendar Switchcalendar (String str) {/
/TODO auto-generated Method stub String time=str.substring (0, ten) + "" +str.substring (11, 18);
Calendar cal = new GregorianCalendar ();
Date data = null;
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); try {data = Sdf.parse (time);//System.out.println (data);} catch (Exception e) {e.printstacktrace ();} cal.settime (Data
); System.out.println (CAL);
Return cal; 
/** * Convert a String type to a Calendar type no time seconds * * @param str * @return/public static Calendar SwitchCalendar2 (String str) {
TODO auto-generated Method stub Calendar cal = new GregorianCalendar ();
Date data = null;
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd"); try {data = Sdf.parse (str);//System.out.println (data);} catch (Exception e) {e.printstacktrace ();} cal.settime (data)
;
System.out.println (CAL);
Return cal; /** * Convert calendar type to java.sql.Date type * * @param cal * @return/public static java.sql.Date switchsqldate (Calendar CA
L) {Date date = Cal.gettime ();
Timestamp datetime = new Timestamp (Date.gettime ());
Java.sql.Date time = new Java.sql.Date (Datetime.gettime ());
System.out.println (time);
return time; /** * Get the day before the current date * * @return/public static String Catchbeforeday () {//Get the day before the current date System.currenttimemillis () -24*6
0*60*1000 (milliseconds of the day) Date date = new Date (System.currenttimemillis ()-86400000); System.out.println ("Current day:" +system.cUrrenttimemillis ());
System.out.println ("day Before:" + (System.currenttimemillis ()-86400000));
Java.text.SimpleDateFormat SDF = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
String time = Sdf.format (date);
return time; /** * Day Date * * @return/public static String Catchday () {java.util.Date date = new Java.util.Date (); Java.text.Sim
Pledateformat SDF = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
String time = Sdf.format (date);
System.out.println ("Today's Date:" +time);
return time; /** * Get the day after the current date * * @return/public static String Catchlaterday () {//Get the day after the current date System.currenttimemillis () -24*60
*60*1000 (number of milliseconds in a day) java.util.Date Date = new Java.util.Date (System.currenttimemillis () + 86400000);
System.out.println ("Current day:" +system.currenttimemillis ());
System.out.println ("Day after:" + (System.currenttimemillis ()-86400000));
Java.text.SimpleDateFormat SDF = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
String time = Sdf.format (date);
System.out.println ("Tomorrow's Date:" +time);
return time; }


/**
*Get the last day of the current month * * @return/public static String Catchlastday () {Calendar cal = Calendar.getinstance ();//Set Date to first day of the month Cal
. Set (calendar.date, 1);
Date rolls back one day, the last day Cal.roll (Calendar.date,-1);
Gets the number of days of the current month int maxday = Cal.get (calendar.date);
Gets the current month int nowmonth = Cal.get (calendar.month) + 1;
String Nowmonth;
if (Nowmonth <) {Nowmonth = "0" + nowmonth;} else {nowmonth = Nowmonth + "";}
Gets the current year int nowyear = Cal.get (calendar.year);
StringBuilder sb = new StringBuilder ();
For stitching Sb.append (nowyear). Append ("-"). Append (Nowmonth). Append ("-"). Append (Maxday);
System.out.println (Sb.tostring ());
return sb.tostring (); /** * Show Time * @param times * @return/public static String ShowTime (long time) {long hour = hour/(* * 1000); Lo
ng minute = (Time-hour * 60 * 60 * 1000)/(60 * 1000);
Long second = (time-hour * * 1000-minute * 60 * 1000)/1000;
if (second >=) {second = second%/minute + = SECOND/60;} if (minute >=) {minute = minute% 60;Hour + = MINUTE/60;
String OK = hour + "hours" + Minute + "min" + second + "seconds";
return OK; /** * String type converted to java.util.Date type * @param string * @return * @throws Exception/public static Date Stringswitchudate
(String string) throws Exception {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Date time = Sdf.parse (string);
return time; /** * Get the last day of the current month * * @return/public static date Lastday () {Calendar cal = Calendar.getinstance ();//Set Date to the first day of the month CA
L.set (calendar.date, 1);

Date rolls back one day, the last day Cal.roll (Calendar.date,-1);
Date Date=cal.gettime ();
System.out.println ("Riqi:" +date);
return date; /** * Get next month * * @return/public static date Catchnextmonthday () {Calendar cal = Calendar.getinstance ();//Set Date to
The first day of the month Cal.set (calendar.date, 1);


Set the month to the next month of the current month Cal.add (Calendar.month, 1);
Date date = Cal.gettime ();
return date; /** * Get current date 31 days after date * * @return/public static date Catchnext31day (date date) {Calendar cal = Calendar.getinstance ()
; Cal.settiMe (date);
Cal.roll (Calendar.month, 1);
Cal.roll (calendar.date, 1);
Date newdate = Cal.gettime ();
return newdate;
 }
}


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.