1 / 42

Dates, Numbers and Currency

Dates, Numbers and Currency. Jiafan Zhou. Related APIs. java.util.Date An instance of Date represents a mutable date and time, to a millisecond. java.util.Calendar Manipulate dates and times java.util.DateFormat Format dates. It has methods to format and parse dates for any locale.

clodia
Download Presentation

Dates, Numbers and Currency

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. Dates, Numbers and Currency Jiafan Zhou

  2. Related APIs • java.util.Date An instance of Date represents a mutable date and time, to a millisecond. • java.util.Calendar Manipulate dates and times • java.util.DateFormatFormat dates. It has methods to format and parse dates for any locale. // Formats a date for the current Locale. Date date = new Date(); DateFormat df = DateFormat.getDateInstance();String str = df.format(date); // Formats a date for the specified Locale DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);String str = df.format(date);// Parses a date/time string to a Date DateFormat df3 = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");Date myDate = df3.parse("Thu Sep 28 07:29:30 BOT 2000");

  3. Related APIs • java.text.NumberFormatFormat numbers and currencies for any locales around the world. Use getInstance() or getNumberInstance() to get the normal number format. Use getIntegerInstance() to get an integer number format. Use getCurrencyInstance() to get the currency number format. Use getPercentInstance() to get a format for displaying percentages. // To format a number for the current Locale, use one of the factory class methods: String myString = NumberFormat.getInstance().format(myNumber); // To format a number for a different Locale, specify it in the call to getInstance():NumberFormat nf = NumberFormat.getInstance(Locale.US);

  4. Related APIs • java.util.Locale The Locale object represents a specific geographical, political or cultural region. The available constructors are: Locale(String language)Locale(String language, String country)Locale(String language, String country, String variant) The Locale class provides a number of convenient constants to create Locale objects for commonly used locales: Locale locale = Locale.US; Used in connection with DateFormat and NumberFormat to format dates, numbers and currency for specific locales.

  5. Use Cases Get the current date and time: Date d = new Date(); String s = d.toString(); Perform date and time calculations in your locale: Locale locale = Locale.getDefault(); Calendar c = Calendar.getInstance(); Date d = c.getTime(); Perform data and time calculations in a different locale: Locale loc = new Locale(language); Calendar c = new Calendar.getInstance(); Get a DateFormat for a specific Locale: DateFormat df = DateFormat.getDateInstance(style, locale); String s = df.format(date);

  6. Homework

  7. Homework • To pass the Oracle certification with good score.

  8. Questions & Answers: 8

  9. Mock Exam Questions: Question 6 Given: • d is a valid, non-null Date object • df is a valid, non-null DateFormat object set to thecurrent locale What outputs the current locales country name and the appropriate version of d’s date? A. Locale loc = Locale.getLocale(); System.out.println(loc.getDisplayCountry()+ “ “+ df.format(d)); B. Locale loc = Locale.getDefault(); System.out.println(loc.getDisplayCountry()+ “ “ + df.format(d)); C. Locale bc = Locale.getLocale(); System.out.println(loc.getDisplayCountry()+ “ “+ df.setDateFormat(d)); D. Locale loc = Locale.getDefault(); System.out.println(loc.getDispbayCountry()+ “ “+ df.setDateFormat(d)); 9

  10. Mock Exam Questions: Question 104 Given: 11. double input = 314159.26; 12. NumberFormat nf= NumberFormat.getInstance(Locale.ITALIAN); 13. String b; 14. //insert code here Which code, inserted at line 14, sets the value of b to 3 14.159,26? A. b = nf.parse( input); B. b = nf.format( input); C. b = nf.equals( input); D. b = nf.parseObject( input); 10

  11. Mock Exam Questions: Question 105 Given: 14. DateFormat df; 15. Date date = new Date(); 16. //insert code here 17. String s = df.format( date); Which two, inserted independently at line 16, allow the code to compile? (Choose two.) A. df= new DateFormat(); B. df= Date.getFormatter(); C. df= date.getFormatter(); D. df= date.getDateFormatter(); E. df= Date.getDateFormatter(); F. df= DateFormat.getInstance(); G. df = DateFormat.getDateInstance(); 11

  12. Mock Exam Questions: Question 106 Given: 12. Date date = new Date(); 13. df.setLocale(Locale.ITALY); 14. String s = df.format(date); The variable df is an object of type DateFormat that has been initialized in line 11. What is the result if this code is run on December 14, 2000? A. The value of s is 14-dic-2004. B. The value of s is Dec 14, 2000. C. An exception is thrown at runtime. D. Compilation fails because of an error in line 13. 12

  13. Mock Exam Questions: Question 107 Given: 33. Date d = new Date(0); 34. String ds = “December 15, 2004”; 35. // insert code here 36. try { 37. d = df.parse(ds); 38. } 39. catch(ParseException e) { 40. System.out.println(”Unable to parse “+ ds); 41. } 42. // insert code here too Which will create the appropriate DateFormat object and add a day to the Date object? A. 35. DateFormat df= DateFormat.getDateFormat(); 42. d.setTime( (60 * 60 * 24) + d.getTime()); B. 35. DateFormat df= DateFormat.getDateInstance(); 42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime()); C. 35. DateFormat df= DateFormat.getDateFormat(); 42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime()); D. 35. DateFormat df= DateFormat.getDateInstance(); 42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime()); 13

  14. Mock Exam Questions: Question 108 Given a valid DateFormat object named df, and 16. Date d = new Date(0L); 17. String ds = “December 15, 2004”; 18. // insert code here What updates d’s value with the date represented by ds? A. 18. d = df.parse(ds); B. 18. d = df.getDate(ds); C. 18. try { 19. d = df.parse(ds); 20. } catch(ParseException e) { }; D. 18. try { 19. d = df.getDate(ds); 20. } catch(ParseException e) { }; 14

  15. Tips & Tricks

  16. About 30% of the questions in the OCJP 6.0 exam are Drag and Drop questions. The answer gets reset if you try to review it. So, always complete and double-check your answer before moving away from a Drag and Drop question.

  17. It would be helpful to solve Drag and Drop questions on a sheet of paper before actually attempting them in the test.

  18. Just reading about the concepts and seeing examples will not make things clear. Make a small example of your own to test every new concept/rule you learn. This will help you understand it better. Also, alter the example and experiment with different possibilities. You will learn and retain much better if you follow this process.

  19. Let us say, you read about the rule that static methods cannot be overridden. It is not very clear what will happen if you attempt to override a static method, whether this will cause a compiler error or not. To explore this, you can make an example similar to this one. class Base{ static void f() { System.out.println("base"); }} class Derived extends Base{ static void f() { System.out.println("derived"); } public static void main(String args[]){ Base base=new Derived(); base.f(); } }

  20. Now try to compile, there are no errors. So, it means that it is not illegal to have the same static method in the base and derived class. But when you invoke the method, the actual method being invoked is the one declared in the base class. This is because the method invoked depends on the declared type of the reference variable. That is why you say that overriding does not happen in this case. If this had been a non-static method, the method invoked would have been the one declared in the subclass. Now things are clear to you. Once this concept is clear, you can start trying different possibilities, such as • What happens if you override a static method with a non-static one or vice versa? • What will happen if you try to declare a static method abstract? • What will happen if you declare a static method to be synchronized? You can think of many options like this. Make small variations to the program and try out all of them. Instead of learning just one concept, you will get a much deeper understanding of many related concepts.

  21. The revised exam features more performance-based questions compared to knowledge-based questions which require memorization of API details. So spend more time on hands-on practice, especially of commonly used methods, and avoid trying to memorize each and every API detail simply by reading.

  22. A question might not pertain to a single objective, but may often test your expertise in various topics. For example, a question from the topic “Collections/Generics' might use autoboxing and the enhanced for loop. So, while you practice one topic, try to associate it with other topics as well.

  23. Be clear about the classes implementing the Collection types like List, Set, and Map. Be aware of which of these types support sorting, which allow duplicates, which give the best performance, and so on.

  24. When you see the wait() and notify() methods, first ensure that they are called within a synchronized context. Also remember that they are defined in the Object class, so they are invoked on the object which is locked and not on the thread itself.

  25. Remember that the scope of a variable declared within simple for, enhanced for, while, or do-while loops is restricted to the loop itself.

  26. Read the choices properly; don't get confused between import statements and import static statements.

  27. Whenever you see classes implementing interfaces, always check if the methods have been defined as public. Also, check if all the methods from the interface have been defined in the class. If this is not the case, make sure the class is declared abstract.

  28. Remember that immutable string objects are given by String class. StringBuilder and StringBuffer both give mutable strings; the only difference between them is that StringBuffer is thread-safe, while the other is not.

  29. When you see the usage of varargs in a method, ensure that the ellipsis is appearing as the last argument to the method.

  30. Remember that the super type of all collections is Collection<?> and not Collection<Object>.

  31. "Inner classes" is one of the most confusing sections. You need to memorize the correct syntax, the different ways of instantiating them, and their access rules. Even questions from other objectives might be using inner classes. So try to practice a lot in this area. Anonymous inner classes are especially prone to errors. Watch out for small syntax errors like the missing semicolon at the end of this anonymous inner class defined inside a method argument.

  32. A subclass can overload the methods defined in both itself and its superclass, don't confuse this with overriding.

  33. Don't confuse between the finalize method and the finally block. They have nothing to do with each other. Any code defined within the finally block will always be called whether an exception happens or not. The only exception is a System.exit call before the control reaches the finally block.

  34. If the subclass does not define any constructors, and is instantiated, then its superclass must have a no-argument constructor since an implicit super() call will be made.

  35. Remember that sleep() and yield() are static methods of the Thread class, and are invoked on the currently executing thread.

  36. Remember that the order in which the threads run is decided by the thread scheduler, though we can request to change the priority. Also, calling the start() method only puts the threads in the runnable state, the run() method gets called only when the thread gets the CPU. Note that calling the run() method directly will not spawn a new thread.

  37. Be clear about the difference between the legal and appropriate uses of assertions. If the assertion syntax is correct then the code is legal. But there are some situations where the use of assertions is inappropriate, even though it is legal. For example, assertion expressions must neither alter the program state nor have any side effects that would not be perceived if assertions were turned off.

  38. The objective on Garbage collection will have questions asking how many objects are eligible for garbage collection at a particular point in the program. The answers to such questions cannot be verified with the help of examples because you cannot force the garbage collector to run. So attempt as many questions as possible on this topic from various mock exams, compare your answers to test your knowledge.

  39. Know the difference between using ++ as a postfix and prefix operator. Also, you must be aware of why && and || are called short-circuit operators and how they differ from & and |.

  40. Once you have gone through all the concepts, start taking as many mock exams as possible to test your knowledge. After taking each mock exam, note down the mistakes you made and work upon them. On the day before the exam, you can go through them once again and ensure that you have mastered your weak areas.

  41. Try to be relaxed and sleep well on the night before the exam. Bring some water in the exam room and drink regularly in order to maintain a fair water balance in your brain.

More Related