Java Code with Highlight
閱讀本文約花費: 1 (分鐘)
from: http://math.hws.edu/eck/cs124/javanotes3/source/Interest1Console.java show the code :[java] /* Simulation of console-I/O program Interest1, using ConsoleApplet as a basis. See the file ConsoleApplet.java for more information. David Eck [email protected] */ public class Interest1Console extends ConsoleApplet { protected String getTitle() { return "Sample program \"Interest1\""; } protected void program() { /* Program computes the amount of interest that is earned on $17,000 invested at an interest rate of 0.07 for one year. The interest and the value of the investment after one year are printed to standard output. */ double principal; // The value of the investment. double rate; // The annual interest rate. double interest; // Interest earned in one year. /* Do the computations. */ principal = 17000; rate = 0.07; interest = principal * rate; // Compute the interest. principal = principal + interest; // Compute value of investment after one year, with interest. // (Note: The new value replaces the old value of principal.) /* Output the results. */ console.put("The interest earned is $"); console.putln(interest); console.put("The value of the investment after one year is $"); console.putln(principal); } } [/java]
Java代码2: [java] public class DateUtil { /** * 只比较日期,不比较小时,分钟,秒 * @param date1 * @param date2 * @return date1 < date2 返回 -1 * date1 = date2 返回 0 * date1 > date2 返回 1 */ public static int compareOnlyDate(Date date1, Date date2) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); cal1.set(Calendar.HOUR_OF_DAY, 0); cal1.set(Calendar.MINUTE, 0); cal1.set(Calendar.SECOND, 0); cal1.set(Calendar.MILLISECOND, 0); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); cal2.set(Calendar.HOUR_OF_DAY, 0); cal2.set(Calendar.MINUTE, 0); cal2.set(Calendar.SECOND, 0); cal2.set(Calendar.MILLISECOND, 0); return cal1.compareTo(cal2); } } [/java]No tags for this post.