JAVA date format tool class DateUtil. java, tool class dateutil. java

Source: Internet
Author: User

JAVA date format tool class DateUtil. java, tool class dateutil. java

DateUtil. java

1 package pers. kangxu. datautils. utils; 2 3 import java. text. simpleDateFormat; 4 import java. util. date; 5 6 import pers. kangxu. datautils. common. exception. defineException; 7 8/** 9*10 * <B> 11 * date processing tool class 12 * </B> 13 * @ author kangxu14 * 15 */16 public class DateUtil {17 18/ ** 19 * String Date to Date format Date 20 * @ param str String Date 21 * @ param dateFormat String Date format 22 * @ return23 */24 public static Date strToDate (String strDate, string dateFormat) {25 SimpleDateFormat sdf = new SimpleDateFormat (dateFormat); 26 Date date = null; 27 try {28 date = sdf. parse (strDate); 29} catch (Exception e) {30 throw new DefineException ("date Format Conversion error"); 31} 32 return date; 33} 34 35/** 36 * convert a date to a String date 37 * @ param date 38 * @ param tarDateFormat date Format 39 * @ return40 */41 public static String dateToStr (date date, string tarDateFormat) {42 43 return new SimpleDateFormat (tarDateFormat ). format (date ); 44} 45 46/** 47 * convert Date Format 48 * @ param strDate string date 49 * @ param srcFormat original format 50 * @ param tarFormat destination format 51 * @ return52 */53 public static String strToStr (String strDate, string srcFormat, String tarFormat) {54 SimpleDateFormat sdf = new SimpleDateFormat (srcFormat); 55 try {56 Date date = sdf. parse (strDate); 57 sdf = new SimpleDateFormat (tarFormat); 58 strDate = sdf. format (date); 59} catch (Exception e) {60 throw new DefineException ("date format Conversion error"); 61} 62 return strDate; 63} 64 65}

Test use
DateUtilTester. java

 1 package pers.kangxu.datautils.test; 2  3 import java.util.Date; 4  5 import pers.kangxu.datautils.utils.DateUtil; 6  7 public class DateUtilTester { 8     public static void main(String[] args) { 9         System.out.println(DateUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:dd"));10         System.out.println(DateUtil.strToStr("2011-1-1 1:1:1","yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss"));11         System.out.println(DateUtil.strToDate("2011-1-1 1:1:1","yyyy-MM-dd HH:mm:ss"));12     }13 14 }

 

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.