Beruflich Dokumente
Kultur Dokumente
y y
Note 1: For information on creating your own class packages or using 3rd-party libraries see the Java Appendix. Note 2: Some programmers find that using an interactive development environment (IDE) such as NetBeans to be very helpful in accessing and using the many extended features that these class libraries provide. Others stay with a basic text editor and develop a more complete understanding of the libraries. It is your call but I recommend a basic text editor for beginning programmers.
example System.currentTimeMillis() retrieves the system clock setting (as a long, in milliseconds counted from January 1, 1970). Some of the available methods are:
currentTime() freeMemory() gc() totalMemory() exit(int status) exec(String cmd) execin(String cmd) getenv(String var) getCWD() getOSName() arraycopy(src[], srcpos, dest[], destpos, len)
Another method called System.getProperty("property_name") allows access to the following system properties:
file.separator line.separator path.separator os.arch os.name os.version user.dir user.home user.name java.class.path java.class.version java.home java.vendor java.vendor.url java.version java.vm.name java.vm.vendor java.vm.version
Note: Many System class methods can throw a SecurityException exception error for safety reasons.
Exponential
Rounding
Methods getInstance() get(fld), getAvailableLocales, getInstance(), getTime(), getTimeZone() add(whichField, intVal), clear(), set(fld,intVal), setTime(), setTimeZone() after(), before(), equals(), isSet()
Mutator
Comparison
The wrapper class Date is used primarily to convert objects into longs for math operations with the getTime() method. Note: Calendar.getTime() converts to Date objects, Date.getTime() converts to long primitives.
Example: timeSpan()
A common request is for a timeSpan() method that computes the difference between two Date or Calendar objects. Since Date has been downgraded to a wrapper with no setField operations, my example uses Calendar objects. The user will have to provide his own input (either sio or file) and output reporter.
import java.util.*; public class Timer { public static void main(String args[]) { // create start and end calendar objects Calendar sTime=Calendar.getInstance();
Calendar eTime=Calendar.getInstance(); // now set times -- add routines to get from sio or file // be sure to verify times are in range !! // adjust times for early start and late finish sTime.set(Calendar.HOUR_OF_DAY,8);sTime.set(Calendar.MINUTE,0); eTime.set(Calendar.HOUR_OF_DAY,16);eTime.set(Calendar.MINUTE,0); long span=timeSpan(sTime,eTime); // adjust time for lunch hour here long secs=span/1000;long mins=secs/60;long hours=mins/60; System.out.println(hours); System.out.println(mins); } // timeSpan (calendarObject,calendarObject) returns long milliseconds public static long timeSpan(Calendar calStart,Calendar calEnd) { Date sTime1,eTime1;long interval,sTime2,eTime2; sTime1=calStart.getTime();eTime1=calEnd.getTime(); // to Date objects sTime2=sTime1.getTime();eTime2=eTime1.getTime(); // to long objects interval=eTime2-sTime2;return interval; } }
k m s w y z : Char D E F G H K M S W /
Hour (1-24) Minute Second Week of year Year Timezone Separator Meaning Day of year Day of week Day of week in month Era (AD or BC) Hour in Day (0-23) Hour in Day (0-11) Month Millisecond Week of month Escape character
{ Date date=new Date(); String rptDate; SimpleDateFormat sdf=new SimpleDateFormat("yyyy MMM dd @ hh:mm aa"); rptDate=sdf.format(date); System.out.println(rptDate+"\n"); } }
% ;
Shows value as a percentage Separates a positive number format (on left) from an optional negative number format (on right) Escapes a reserved character so it appears literally in the output
'
Accessor
Mutator
setPositivePrefix(newValue), setPositiveSuffix(newValue) Boolean Instance equals(obj), isDecimalSeparatorAlwaysShown() applyLocalizedPattern(pattern), applyPattern(pattern), format(number), String toLocalizedPattern(), String toPattern(),