[Java] Time to get a custom format

Source: Internet
Author: User
Tags dateformat


Import Java.io.ioexception;import Java.text.dateformat;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.arraylist;import Java.util.calendar;import Java.util.Date;import Java.util.list;import cn.com.agree.ab.trade.core.trade;/** * <DL> * <DT><B> function Description </b></dt > * String Processing Tool * </DL> * * @author * @since 2015-6-3 */public class Dateutil {/** * <b> feature description </b> get custom grid             Type of time * * @param format * format * <p> * <b> for example:</b><br> * Use examples to illustrate String a=netcheck.getdatastring ("YyyyMMdd"); * </p> */public static string getdatastring (string format) {DateFormat DateFormat = new SimpleDateFormat (format); return Dateformat.format (New Date ());} public static string Getnextday (date date, string format) {Calendar calendar = calendar.getinstance (); Calendar.settime ( date); Calendar.add (Calendar.day_of_month, -7);d ate = Calendar.gettime ();D Ateformat DateFormat = New SimpleDateFormat (format); return Dateformat.format (date);} public static string Getformatdate (String datestr) throws ParseException {SimpleDateFormat df = new SimpleDateFormat ("yyy YMMDDHHMMSS ");D ate Date = Null;date = Df.parse (datestr);D ateformat dateformat = new SimpleDateFormat (" Yyyy-mm-dd HH:mm: SS "); return Dateformat.format (date);} public static string Getnextday (date date, string format, int days) {Calendar calendar = calendar.getinstance (); CALENDAR.S Ettime (date), Calendar.add (Calendar.day_of_month, days);d ate = Calendar.gettime ();D ateformat DateFormat = new SimpleDateFormat (format); return Dateformat.format (date);} public static list<string> getmonthweekends (trade trade,int month,int year) throws Ioexception{list<string > hostdays = new arraylist<string> (); Calendar cal = Calendar.getinstance (); Cal.set (Calendar.year, year); Cal.set (Calendar.month, month-1); Cal.set ( Calendar.day_of_month, 1); int startIndex = Cal.get (Calendar.day_of_week)-1; 0~6 = Sunday ~ Saturday int EndinDex = StartIndex + getlastdayofmonth (year, month)-1;int startday = 1;for (int i = 0; i <; i++) {if (I >= starti Ndex && I <= endIndex) {cal.set (calendar.day_of_month, StartDay); if (Cal.get (calendar.day_of_week) ==1| | Cal.get (Calendar.day_of_week) ==7) {Hostdays.add (startday+ "");} startday++;} }return Hostdays;} public static Boolean Isleapyear (int.) {return (year% 4 = = 0 && Year% 100! = 0) | | (Year% 400 = = 0);} public static int Getlastdayofmonth (int year, int month) {if (month = = 1 | | month = = 3 | | month = = 5 | | month = = 7 | | mont H = = 8| | Month = = 10 | | month = =) {return 31;} if (month = = 4 | | month = = 6 | | month = = 9 | | month = = one) {return 30;} if (month = = 2) {if (Isleapyear (year)) {return;} else {return 28;}} return 0;}}




[Java] Time to get a custom format

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.