Java24:java API regular function; stringbuilder;date

Source: Internet
Author: User
Tags array to string time zones

Java Regular Functionality

In Java.util.regex.Pattern


Java String Support Regular

Mathch (Regular expression)

Matches a string to a regular expression and returns true if the match succeeds otherwise false

Package Day24;import Java.util.scanner;import Org.apache.commons.lang.stringutils;public class Demo01 {public static void Main (string[] args) throws Exception{scanner in =new Scanner (system.in); String name = In.nextline (); Boolean pass = Name.matches ("^\\w{8,10}$"); System.out.print (pass);}}
Package Day24;public class Demo02 {public static void main (string[] args) throws exception{string str = "2016-01-13,192.16 8.0.100,admin,[email protected] "; string[] data = Str.split (",");//Convert to Array System.out.println (arrays.tostring (data) as specified string); String s = Str.replaceall (",", "|"); /Replace all matching strings System.out.println (s); char[] CHS = S.tochararray ();//character group to character array System.out.println (Arrays.tostring (CHS)) ; string ss = new string (chs,0,10);//convert character array to string can be added range System.out.println (ss); ss = new string (CHS); SYSTEM.OUT.PRINTLN (ss);}}


String cannot be made into strings

StringBuilder variable string


The object generated by this class will have a length of 16 characters by default

string{char[]+ algorithm (does not change content, often returns new strings)}

Stringbuilder{char[] + algorithm (changed the content of char[], when capacity is insufficient, to expand operation)}


String and StringBuilder can be converted to each other

ToString can convert StringBuilder to string


Package Day01;public class Demo04 {public static void main (string[] args) throws Exception{//stringbuilder's API method is for its internal CH Ar[] content to be StringBuilder SB =new StringBuilder () sb.append ("Wave") Append ("Romantic"). Append ("Romantic"). Append ("Romantic Steal"). Append ("Romantic Laughter"). Delete (0, 1). Insert (0, "LMDTX"); System.out.println (SB);}}



The process of string connection in Java is implemented using the StringBuilder

A large number of string computation calculations are done using StringBuilder.

Package Day24;public class Demo05 {public static void main (string[] args) {System.out.println (Test2 (500000)); System.out.println (Test1 (500000));} public static long test1 (int n) {Long start = System.currenttimemillis (); String s = ""; for (int i = 1; i<n;i++) {s+= "A";} Long end = System.currenttimemillis (); return end-start;} public static long test2 (int n) {Long start = System.currenttimemillis (); StringBuilder sb = new StringBuilder (); for (int i = 0;i<n;i++) {sb.append ("A");} String s = sb.tostring (); Long end = System.currenttimemillis (); return end-start;}}


StringBuffer started with a slightly slower performance of thread-safe synchronization. Multithreading using this

StringBuilder java1.5 is non-thread-safe, and the performance of concurrent processing is slightly faster. Single thread Use this



Char[] String StringBuilder that represents a string in Java

Char[] Just character data, no algorithms, can be implemented by themselves, trouble

String is an immutable string and is the most commonly used string, and most Java character data is output using a string

StringBuilder is a mutable string, evaluated in the contents of a string, implemented using StringBuilder


Time in Java


Distance from time calculation: is the number of milliseconds +--related to the calendar rules

is an integer of type long, which is the number of milliseconds GMT19700101 to the present


Java.util.Date encapsulates the ad calendar algorithm

Date{long + AD Calendar algorithm} has drawbacks: 2000 issues; time zones and national issues

Not suitable for time calculations

Use calendar overrides

Date API method

GetYear (); 2000-year question, 2-digit year returned

GetMonth ();

GetDate ();

GetDay ();

GetTime (); Returns a long number of milliseconds

SetTime (long); Number of milliseconds to modify time

package day24;import java.util.date;import java.text.simpledateformat;public class  Demo06 {public static void main (String[] args) {date date = new  Date ();//default is the current time int year =date.getyear () +1900;int month = date.getmonth () +1;int  d = date.getdate (); Int day = date.getday ();int hours =  Date.gethours (); Int minutes = date.getminutes (); Int seconds = date.getseconds (); System.out.println (year + "-" +month+ "-" +d + " " +hours + ":" +minutes+ ":" +seconds "); Simpledateformat fmt = new simpledateformat ("Yyyy-mm-dd e hh:mm:ss");//yyyy  2016//yy 16//mm 01//m  1//dd 13//d  6//e Wednesday//aa-  PM//h24 Hour System// H12 hour//m//s second//string str = fmt.format (date); System.out.println (str); System.out.println (Fmt.format (0));//long  directly to him}}


This article from the "Romantic Smile" blog, reproduced please contact the author!

Java24:java API regular function; stringbuilder;date

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.