130 likes | 275 Vues
Working with Date. ISYS 350. Date Class, java.util.Date. The class Date represents a specific instant in time, represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000. For a date before 1/1/1970, it is a negative number
E N D
Working with Date ISYS 350
Date Class, java.util.Date • The class Date represents a specific instant in time, represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000. • For a date before 1/1/1970, it is a negative number • Constructor without argument: Current date • Date myDate = new Date(); • System.out.println("Today is: " + myDate.toString()); • Constructor without argument: • Date(long date) Constructs a Date object using the given milliseconds time value. • It is basically impossible to know precisely how many milliseconds • GetTime method: Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date. • Note: import java.util.Date;
GregorianCalendar Class • It has a constructor that lets user to specify a specific date: • GregorianCalendar ThisDate = new GregorianCalendar(2009,Calendar.SEPTEMBER,10); • Has fields to get info about date: • YEAR, MONTH,DAY_OF_MONTH , DAY_OF_WEEK, WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, AM_PM, HOUR, HOUR_OF_DAY, etc. • System.out.println("Year: " + ThisDate.get(Calendar.YEAR));
GregorianCalendar’sGetTime method • public final Date getTime() • Returns a Date object representing this Calendar's date. • This is different from the Date class getTime method. • Note: We can use GregorianCalendar object to create a date; then use its getTime method to create a Date object.
Compute Days to Christmas Date toDay = new Date(); GregorianCalendar ThisDate = new GregorianCalendar(2009,Calendar.DECEMBER,25); Date ChristMas=ThisDate.getTime(); double datediff= (ChristMas.getTime()-toDay.getTime())/(24*60*60*1000); System.out.println("days to Christmas: " + datediff);
Compute Age from Birthday Method 1: Using Date object’s GetTime method: Date toDay = new Date(); GregorianCalendar BirthDay = new GregorianCalendar(1990,Calendar.DECEMBER,25); Date DoB=BirthDay.getTime(); double Age= (toDay.getTime()-DoB.getTime())/(24*60*60*1000)/365.25; System.out.println("Age: " + Age); Method 2: Using GregorianCalendar’s YEAR property GregorianCalendar TodayDate= new GregorianCalendar(); GregorianCalendar BirthDay = new GregorianCalendar(1990,Calendar.DECEMBER,25); Age= TodayDate.get(Calendar.YEAR) - BirthDay.get(Calendar.YEAR); System.out.println("Age: " + Age);
String Class Methods • toLowerCase() • toUpperCase() • substring(intbeginIndex) • "unhappy".substring(2) returns "happy" • 0-based index • substring(intbeginIndex, intendIndex) • The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. • "hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile" • length() • indexOf( char)
Full name format: First Name + Space + Last NameChange the Full Name to proper format where the first letter of First Name and Last Name changed to uppercase and other letters in lower case Example: “david chao” -> “David Chao”
Code Example public static void main(String[] args) { // TODO code application logic here Scanner sc=new Scanner(System.in); System.out.println("enter full name:"); String FullName=sc.nextLine(); int spaceIndex = FullName.indexOf(" "); String firstName=FullName.substring(0,spaceIndex); String lastName=FullName.substring(spaceIndex+1); String newFullName=proper(firstName) + " " + proper(lastName); System.out.println("Proepr full name:" + newFullName); } public static String proper(String Name){ return Name.substring(0,1).toUpperCase()+ Name.substring(1).toLowerCase(); }
Character class • isDigit(char) • isLetter(char)
Is a string containing digits only? public static boolean isNumeric(String s){ int i=0; boolean foundLetter=false; for (i=0;i<=s.length()-1;++i){ if (Character.isLetter(s.charAt(i))){ foundLetter=true; break; } } if (foundLetter) return false; else return true; }
Exercise • Write a isAlpha procedure to determine if a string contains only letters of A-Z. • Social Security Number format is : ddd-dd-dddd where d is a digit of 0-9. Write a procedure to test if the social security number entered by a user is valid.