Sampledateformat date Formatting

Source: Internet
Author: User

The date we generated may not be the format we want, so we need to convert it with the format method of the Sampledateformat class.

Sampledateformat is a common date class under the Java.text package

A common method of this common class, we often use 2

parse(String text, ParsePosition pos)
Parses the text of a string, generated Date .

format(Date date, StringBuffer toAppendTo, FieldPosition pos)
Converts the given Date format to a date/time string and adds the result to the given StringBuffer .

Example: Enter the member's birthday, the form must be "month/day", such as "09/12", the input password must be in the 6~10 bit, allow the user to repeat the entry, until the input is correct so far

The code is as follows:

//User Class Packagechangyonglei.homework;/*** Enter the member's birthday, the form must be "month/day", such as "09/12", the input password must be in the 6~10 bit, allow the user to repeat the entry, until the input is correct so far*/Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date; Public classvip{PrivateString vipdate;//member Birthday    PrivateString password;//member Password     PublicVip () {} PublicVip (String vipdate, string password) { This. vipdate =vipdate;  This. Password =password; }     PublicString getvipdate () {returnvipdate; }     Public voidsetvipdate (String vipdate) {//Specify FormatSimpleDateFormat date=NewSimpleDateFormat ("Mm/dd"); Try{Date date1=date.parse (vipdate);//interprets the text of a string, generating a dateDate.format (date1);//formats the given date as a date/time string that we define             This. vipdate=vipdate; }Catch(Exception e) {System.err.println ("Date format entered incorrectly!" "); }    }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) {if(Password.length () >=6 && password.length () <=10){             This. Password =password; }Else{System.err.println ("Password form input wrong!" "); }            }    }//Test Class Packagechangyonglei.homework;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Scanner;/*** Verification of the validity of input information * Enter the member's birthday, the form must be "month/day", such as "09/12", the password must be in the 6~10 bit, allow users to repeat the input, until entered correctly *@author0 **/ Public classTESTVIP { Public Static voidMain (string[] args)throwsparseexception {Scanner input=NewScanner (system.in); VIP VIP=NewVip ();        String Day; //Enter the birthday first, if it is empty, continue to enter the birthday, if not empty, then start to enter the password        BooleanFlag; //make a member birthday verification         Do{System.out.println ("Please enter the birthday <mm/dd>:"); Day=Input.next ();            Vip.setvipdate (day); if(Vip.getvipdate ()! =NULL){                //for password verificationString pwd;  Do{System.out.println ("Please enter your password:"); PWD=Input.next ();                Vip.setpassword (PWD); } while(Vip.getpassword () = =NULL); Flag=false; }Else{flag=true; }            //If the rule executes the date conversion, then it can set the value, if it does not conform to the rule, is empty, so we judge the date is empty to continue the loop} while(flag); System.out.println ("The program is over!" "); }}

Results:

Sampledateformat date Formatting

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.