1 / 13

Working with Date

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

Download Presentation

Working with Date

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Date ISYS 350

  2. 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;

  3. 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));

  4. 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.

  5. 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);

  6. 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);

  7. Working with String

  8. 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)

  9. 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”

  10. 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(); }

  11. Character class • isDigit(char) • isLetter(char)

  12. 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; }

  13. 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.

More Related