Java SimpleDateFormat Tool Class

Source: Internet
Author: User
Tags string methods

Package Animaldemo;import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Date;// Date Tool class public class Dateutil {public static final String defalt_adte_pattern= "Yyyy-mm-dd HH:mm:ss";p ublic static void Main ( String[] args) throws ParseException {System.out.println (datestring (New Date (), "Yyyy-mm-dd HH:mm:ss")); System.out.println (DateString (New Date (), null));} Private Dateutil () {}//-----------------------------------------------------------------------/* Determines whether the string is null or is an empty string */private the static Boolean IsBlank (String str) {return!haslength (str);} /* * Determine string non-null * Judge string NOT NULL, neither reference is NULL, nor null character * */private static Boolean haslength (String str) {///to encapsulate these two null-judged string methods into a class, it can be generalized.  if (str!=null && "". Equals (Str.replace ("", "")) {//return true;//}//return false;return str!=null && ". Equals (Str.replace (" "") ");//Remove blank space to return true}//-------------------------------------------------------------- ---------//Format operation:--------------the date type >string type public static String datestring(Date date) {return datestring (date,null);} Format operation: the date type-------------->string type public static String datestring (date date,string pattern) {SimpleDateFormat Sdf=new SimpleDateFormat (); if (IsBlank (pattern)) {Pattern=defalt_adte_pattern;} Sdf.applypattern (pattern); return Sdf.format (date);} The parse operation puts the type String-------------->date type public static date StringDate (string date) throws Parseexception{return StringDate (date,null);} The parse operation puts the type string-------------->date type public static Date stringdate (string date,string pattern) throws Parseexception{simpledateformat sdf=new SimpleDateFormat (); if (IsBlank (pattern)) {Pattern=defalt_adte_pattern;} return Sdf.parse (date);}}

Import Java.util.calendar;import Java.util.date;public class Calendardemo {public static void main (string[] args) {//TOD O auto-generated method stub//requirements: Query last week ... Information about how to represent the last week. Date Current=new date ();//The current system time as the calendar Time C=calendar.getinstance (); c.settime;//Increase the day, the time, the Minutes, The second is set to 0c.add (Calendar.day_of_month, 1),//Plus one day c.set (calendar.hour_of_day, 0);//set to 0 c.set (calendar.minute, 0);// Set the score to 0 c.set (calendar.second, 0);//Set the second to 0Date Endtime=c.gettime (); C.add (Calendar.day_of_month, -7);D ate begintime= C.gettime (); System.out.println ("End Time:" +endtime.tolocalestring ()); System.out.println ("Start Time:" +begintime.tolocalestring ());}}

Java SimpleDateFormat Tool Class

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.