The internal representation of a given date is not evaluated until it is really needed, that is, until you try to access it with these getters. However, the best way to parse is SimpleDateFormat.
EDIT (added for a summary of the comments below and to better clarify my answer).
The calendar works this way to increase efficiency: instead of recounting everithing every time you call setter, it waits until you call getter.
The calendar should be used mainly for calculating the date (see add() and roll() ), but you use it for parsing and formatting: these tasks are better done with SimpleDateFormat, so I say that your use of the Calendar is not elegant.
See this example:
private static void convertTimeZone(String date, String time, TimeZone fromTimezone, TimeZone toTimeZone) throws ParseException { SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm"); df.setTimeZone(fromTimezone); Date d = df.parse(date + " " + time); df.setTimeZone(toTimeZone); System.out.println("Time in " + toTimeZone.getDisplayName() + " : " + df.format(d)); }
I repeated your method only with SimpleDateFormat. My method is smaller, there is no splitting logic (it is hidden in parse() ), and it is also processed in a simpler way. In addition, the date format is expressed in a compact and standard way that can be easily internationalized using the ResourceBundle.
Also note that time zone conversion is just a formatting task: the internal representation of the syntax date does not change.
source share