World class Hard Drive Recovery and renowned raid recovery services

WestNIC provides reliable web hosting services

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.

Subscribe to this FAQ: RSS news feed

Storage structures

Q: What is the first argument in the Hashtable put() method?

A: You should download the Java API documentation, which details all standard classes and their method parameters. The first parameter in a Hashtable's put method is the key by which the value in the second argument will be stored.

To retrieve the stored value, use the get(Object) method, where the object parameter is a reference to the key.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What is the difference between Hashtable and HashMap?

A: In Java 2 Hashtable fulfills the Map interface like HashMap. The main difference between Hashtable and HashMap is that HashMap is not synchronized and allows a null key to be used and null values to be stored.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
What is the difference between Hashtable and HashMap?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How do I get the keys from a Map?

A: To get the keys in a map rather than the values, use the keySet() method, which returns a java.util.Set object that contains all the keys. You can then get an Iterator from the key set, or process the keys using other Set methods.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Why is Vector a legacy class?

A: Vector is a legacy class that was adapted in Java Software Development Kit version 1.2 to integrate it with the more contemporary collections framework. Vector now implements the Collection interface.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Working with dates and calendars

Q: How do I insert slashes in a date?

A: To format Date types in Java, you should use a java.text.SimpleDateFormat. This class takes a time pattern string and applies it to the given date like a template. To create a date with slashes, you might use the pattern "dd/MM/YYYY" for instance.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
How do I insert slashes in a date?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I convert a string into a Date object?

A: The recommended way to parse a String representation of a date to a Date object is to use the java.text.SimpleDateFormat class parse(String) method. In its simplest form, the class is instantiated with a String pattern argument that defines the general syntax of the date to be parsed. In the example below, the W3C date time format for year, month and day is represented by the pattern "yyyy-MM-dd" and is used to parse the date string "2008-09-20".

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
How can I convert a string into a Date object?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I get the difference between two dates in days?

A: To compare dates by day, deduct the millisecond time of the earlier date from the later one and divide the result by the number of milliseconds in a day. This method checks the dates are not the same and uses the after(Date) method to check the deduction is the right way round. For finer comparisons by hour or minute, it is important to check any possible timezone offset too.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
How can I get the difference between two dates in days?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Why isn't my custom Calendar class called?

A: If you have a broad import statement like import java.util.*;, the Java Virtual Machine will adopt the standard distribution version of any named class, such as java.util.Calendar. You should limit the scope of your import statements and use the fully qualified name of the custom Calendar class.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
Why isn't my custom Calendar class called?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Why do we use Calendar.getInstance()?

A: The Calendar class is abstract and instances must be obtained from the static getInstance() method so that they are created with an appropriate locale. Calendars must be created with a specific Locale reference to take account of regional time zones and any seasonal daylight saving changes when getting dates, including AM or PM values and time relative to Greenwich Meantime (GMT).

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Add this page to your chosen social bookmarking service

Style warning - please read

Home · CSS · Java · Javascript · HTML · Help · Log