The 10th common tool class in Java

Source: Internet
Author: User
Tags dateformat set time wrapper

Sort out the last part of the week. Although not many things, but later can often come back to see.

Personal feeling or code is best to express what you want to remember, not enough words to add the comments. Messy concept Baidu on the same, only the code belongs to your own, is the only, but also you should stay.

Packing class

The general data types we use are also known as value types. This data type is stored in the stack and is not part of the object category. But Java is an object-oriented language, everything objects, nature can not exclude these value types, so there is a wrapper class.

The so-called wrapper class, is to use the value type variable we used to carry out a wrapper, so that we can manipulate it by the way of the object, let it have its own properties and methods. The process of wrapping value type data into reference data types is also called boxing, which is called unboxing.

The corresponding relationship of the wrapper class:

In general, our compilers automatically pack and remove data:

1 int aInt = 0; 2 Integer ainteger = aInt;    // Automatic Boxing 3 int ai2i = Ainteger;        // Automatic Unpacking
automatic packing and unpackingCommon methods and conversions to and from strings (for example, integer)
    • static int parseInt(String s)  将字符串转成相应int数据类型的数
    • static Integer valueOf(int i)  装箱方法

Similarly, the string class provides the appropriate method for converting the basic data types to string:static String valueOf(int i)

Date Tool Class 1, class date

Common methods:

    • Date(long date)  默认无参构造函数就不说了,其获取的是系统当前时间。这个带参构造函数传入一个long类型的值,代表着时间戳,即1970.1.1开始计算的毫秒数
    • after(Date when)  跟另外一个Date对象比较,返回是否在when之后。
    • before(Date when) 跟另外一个Date对象比较,返回是否在when之前。
    • getTime()  返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。

The method of operation of the date class is very small, mainly because the Calendar class and the DateFormat class, so that it only as a very chicken existence, in addition to get the number of milliseconds, basically no other function, the operation of the date object is referred to DateFormat to handle.

2, Class SimpleDateFormat

From the name we can see that this is a time to format the class, through which we can specify the date object in a specific format to display, you can also make a particular format of the string into a Date object.

Some pattern letters:

    • Y years
    • M-month
    • D Day
    • Number of weeks in W year
    • Number of weeks in W month
    • E (days in the week) week
    • H Hours (0~23)
    • H Hours (1~12)
    • S number of seconds
    • S milliseconds

Common methods:

    • SimpleDateFormat() 用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat
    • SimpleDateFormat(String pattern) 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat
    • applyPattern(String pattern) 将给定模式字符串应用于此日期格式。
    • format(Date date) 将日期格式化成日期/时间字符串。(jdk1.8)
    • parse(String text) 从给定字符串的开始解析文本以生成日期。(jdk1.8)
1 New Date (); 2 // instantiate a SimpleDateFormat object and pass in a specific format 3 New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss E");     4 // Convert Date to string after formatting 5 String nowdatestring = Spdf.format (date); 6 // Print formatted date string     7 System.out.println (nowdatestring);
implementing Date-to-string with SimpleDateFormat
1 2018-07-22 20:20:24 Sunday
Printing Results 1
1 //apply a new format to the Spdf2Spdf.applypattern ("yyyy-mm");3 Try {4     //converts a specific format string into a Date object, throwing ParseException5Date str2date = Spdf.parse ("2018-9");6     //gets the converted timestamp7     LongStr2datemills =str2date.gettime ();8 System.out.println (str2datemills);9}Catch(ParseException e) {TenSYSTEM.OUT.PRINTLN ("date format is wrong! "); One}
implementing string to date via SimpleDateFormat
1 1535731200000
Printing Results 23. Class Calendar (abstract)

Calendar analogy Date class feature to enrich a lot, it provides a lot of time field acquisition and operation method, but it can not be simpledateformat operation, so can only write their own algorithm for its time and the format of the output and the conversion between the string.

Some event fields (static constants, which are used in methods such as GET, set, etc.):

    • DATE 指示一个月中的某天。
    • DAY_OF_MONTH 指示一个月中的某天。
    • DAY_OF_YEAR 指示当前年中的天数。
    • HOUR 指示上午或下午的小时(1~12)。
    • HOUR_OF_DAY 指示一天中的小时(0~23)。  
    • YEAR 年
    • MONTH 月
    • DAY_OF_WEEK 指示一个星期中的某天。

Common methods:

    • Add (Int field, Int amount)   increment or decrement a time field
    • Get (Int field)   Returns the value of the given Calendar field
    • getactualmaximum (Int field)   Returns the current maximum value for a given time field
    • getactualminimum (Int field)    Returns the current minimum value for a given time field
    • getinstance ()   Returns a calendar instance. Because the calendar is an abstract class and cannot be instantiated, you can only get an instance of it by using this method
    • getTime ()   Returns a Date object with the same time as the current calendar object
    • gettimeinmillis () return timestamp
    • Set (Int field, Int value)   Set the given Calendar field to the given value
    • settime (date date)   set time for this Calendar with the given Date
    • settimeinmillis (long millis)   Set the current time value for this Calendar with a given Long value

Using it is simple, here is only one way to convert a string to the calendar format: First, the string is converted to a Date object by SimpleDateFormat, and the calendar is set by the SetTime method.

1System.out.println ("Please enter a date (XXXX-XX-XX):");2String str =Scanner.next ();3 Try {            4     //string to date and set calendar by date5 calendar.settime (Sdf.parse (str));6}Catch(ParseException e) {7SYSTEM.OUT.PRINTLN ("Time format Error! ");8     return;9}
setting Claendar objects with console inputArrays Tool Class

Collections Tool Class

The 10th common tool class in Java

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.