Download presentation
Presentation is loading. Please wait.
Published byMarcus Charles Modified over 9 years ago
1
Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types
2
StringBuffer Why we need it?
3
constructors StringBufferStringBuffer() Constructs a string buffer with no characters in it and an initial capacity of 16 characters. StringBufferStringBuffer(int length) Constructs a string buffer with no characters in it and an initial capacity specified by the length argument.
4
StringBufferStringBuffer(String str) Constructs a string buffer so that it represents the same sequence of characters as the string argument; in other words, the initial contents of the string buffer is a copy of the argument string.String
5
Methods public int length() Returns the length (character count) of this string buffer public int capacity() Returns the current capacity of the String buffer. The capacity is the amount of storage available.
6
public void setLength(int newLength) Sets the length of this String buffer. This string buffer is altered to represent a new character sequence whose length is specified by the argument. if the newLength argument is less than the current length of the string buffer, the string buffer is truncated to contain exactly the number of characters given by the newLength argument. If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended to the string buffer so that length becomes the newLength argument.
7
public StringBuffer append(String str)StringBufferString Appends the string to this string buffer. The characters of the String argument are appended, in order, to the contents of this string buffer, increasing the length of this string buffer by the length of the argument. If str is null, then the four characters "null" are appended to this string buffer.
8
Actually there are many versions of append method, i.e., the parameter could be : boolean(“0”/”1”?), char, char[], int, long, double, float, StringBuffer, Object
9
Used to implement the string concatenation operator +. x = "a" + 4 + "c" is compiled to the equivalent of: x = new StringBuffer().append("a"). append(4).append("c").toString()
10
public StringBuffer delete(int start, int end)StringBuffer Removes the characters in a substring of this StringBuffer. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists. If start is equal to end, no changes are made.
11
public StringBuffer replace(int start, int end, String str)StringBuffer String Replaces the characters in a substring of this StringBuffer with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the StringBuffer if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. The StringBuffer will be lengthened to accommodate the specified String if necessary.
12
public StringBuffer insert(int offset, String str)StringBuffer String Inserts the string into this string buffer. The characters of the String argument are inserted, in order, into this string buffer at the indicated offset, moving up any characters originally above that position and increasing the length of this string buffer by the length of the argument. If str is null, then the four characters "null" are inserted into this string buffer.
13
public StringBuffer reverse()StringBuffer The character sequence contained in this string buffer is replaced by the reverse of the sequence.
14
java.util.StringTokenizer allows an application to break a string into tokens. A delimiters is a character that separates tokens the default delimiter set is " \t\n\r\f"
15
constructor public StringTokenizer(String str)String Constructs a string tokenizer for the specified string. public StringTokenizer(String str, String delim)String Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters.
16
public StringTokenizer(String str, String delim, boolean returnDelims)String Constructs a string tokenizer for the specified string. All characters in the delim argument are the delimiters. If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens.
17
public boolean hasMoreTokens() Tests if there are more tokens available from this tokenizer's string. If this method returns true, then a subsequent call to nextToken with no argument will successfully return a token.
18
public String nextToken()String Returns the next token from this string tokenizer.
19
public int countTokens() Returns the number of tokens remaining in the string using the current delimiter set.
20
java.util.Date The class Date represents a specific instant in time, with millisecond precision.
21
constructors public Date() (extended ) Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
22
public Date(long date) Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
23
public String toString()String Converts this Date object to a String of the form: dow mon dd hh:mm:ss zzz yyyy
24
methods public boolean after(Date when)Date Tests if this date is after the specified date. public boolean before(Date when)Date Tests if this date is before the specified date.
25
public boolean equals(Object obj)Object Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.
26
public long getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.
27
java.text.SimpleDateFormat format (date -> text) parse (text -> date) dates in a locale-sensitive manner
28
Date and Time Patterns G Era designator Text ADText y Year Year 1996; 96Year M Month in year Month July; Jul; 07Month w Week in year Number 27Number W Week in month Number 2Number D Day in year Number 89Number d Day in month Number 10Number F Day of week in month Number 2Number E Day in week Text Tuesday; TueText a Am/pm marker Text PM HText
29
H Hour in day (0-23) Number 0Number h Hour in am/pm (1-12) Number 12Number k Hour in day (1-24) Number 24Number K Hour in am/pm (0-11) Number 0Number m Minute in hour Number 30Number s Second in minute Number 55Number S Millisecond Number 978Number z Time zone General time zone Pacific Standard Time; PST; GMT-08:00General time zone *Z Time zone RFC 822 time zoneRFC 822 time zone -0800
30
"yyyy.MM.dd G 'at' HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT "EEE, MMM d, ''yy" Wed, Jul 4, '01 "h:mm a" 12:08 PM "hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
31
"K:mm a, z" 0:08 PM, PDT "yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM "EEE, d MMM yyyy HH:mm:ss Z" Wed, 4 Jul 2001 12:08:56 -0700 "yyMMddHHmmssZ" 010704120856-0700
32
public SimpleDateFormat() Constructs a SimpleDateFormat using the default pattern and the default date format symbols for the default locale public SimpleDateFormat(String pattern)String Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default locale.
33
public final String format(Date date)StringDate Formats a Date into a date/time string.
34
public Date parse(String text, ParsePosition pos)DateString ParsePosition Parses text starting at the index given by pos. from a string to produce a Date. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed date is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.
35
SimpleDateFormat sdf = new SimpleDateFormat ( "K:mm a, z" ); myString = sdf.format(myDate);
36
java.util.Calendar for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on.
37
Field public static final int JANUARY - DECEMBERJANUARY DECEMBER public static final int SUNDAY – SATURDAYSUNDAY SATURDAY
38
public static final int DAY_OF_YEAR(takes values 1-365), DAY_OF_YEAR public static final int DAY_OF_MONTH(takes values 1-31) public static final int DAY_OF_MONTH DAY_OF_WEEK DAY_OF_WEEK (takes values SUNDAY- SATURDAY )
39
public static final int YEAR YEAR public static final int MONTHMONTH public static final int HOUR public static final int HOUR_OF_DAY
40
public static final int MINUTE public static final int SECOND public static final int MILLISECOND
41
costructors protected Calendar() Constructs a Calendar with the default time zone and locale. protected Calendar(TimeZone zone, Locale aLocale)TimeZone Locale Constructs a calendar with the specified time zone and locale.
42
methods public static Calendar getInstance()Calendar Gets a calendar based on the current time using the default time zone and locale.
43
public final void set(int year, int month, int date) Sets the values for the fields year, month, and date. Previous values of other fields are retained. If this is not desired, call clear first.
44
public final void set(int year, int month, int date, int hour, int minute, int second) Sets the values for the fields year, month, date, hour, minute, and second. Previous values of other fields are retained. If this is not desired, call clear first.
45
public final void clear() Clears the values of all the time fields. public final void clear(int field) Clears the value in the given time field. Q: what kind of field should be put in?
46
public final void setTime(Date date)Date Sets this Calendar's current time with the given Date. public final Date getTime()Date Gets this Calendar's current time.
47
Reviewing Questions What is the difference between length( ) and capacity( ) method of StringBuffer? Why we use sb.length() to get the length of a StringBuffer instance sb while using s.length for a String instance s? When should we use append(..) and insert(.. ) method of StringBuffer ?
48
What is the use of returnDelims In the following method ? pulbic StringTokenizer(String str, String delim, boolean returnDelims)String What should be noted when using the Calendar class set(……) method? Why?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.