سؤال

I would set a TimeZone for Calendar instance but i'm not able to do it. I've try with:

DateFormat dateFormat = new SimpleDateFormat("dd//MM/yyyy HH:mm:ss");
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/Rosario"));
System.out.println(cal.getTime());

(i've used "America/Rosario" randomly), but i obtain always my current time. What's the right mode to do that?

هل كانت مفيدة؟

المحلول

You should set timezone in DateFormat object:

DateFormat df = new SimpleDateFormat("dd//MM/yyyy HH:mm:ss", Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("America/Rosario"));

// Will print the formatted date-time in the America/Rosario timezone  
System.out.println(df.format(new Date()));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top