I'm trying to instantiate a GregorianCalendar with TimeZone GMT, but whenever I call the getTime () method, it gives me time in the local TimeZone. Here is my code:
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); System.out.println(cal.getTime());
The result I get is the following:
Sat Nov 28 19:55:49 PKT 2009
Please, help!
I'm not sure if this answers your question, but this is one way to get "now" in GMT.
import java.text.* import java.util.* Calendar cal = new GregorianCalendar(); Date date = cal.getTime(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z"); formatter.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println(formatter.format(date));
See Javadoc on SimpleDateFormat for different templates. In addition, you can consider Joda Time as it is far superior to dates and times.
GregorianCalendar, Date, / toString println.
GregorianCalendar
Date
toString
println
, DateFormat - SimpleDateFormat, , , .
DateFormat
SimpleDateFormat
, getInstance... factory DateFormat.
getInstance...
setTimeZone DateFormat ( SimpleDateFormat, ).
setTimeZone
Source: https://habr.com/ru/post/1724169/More articles:PHP and scheduled tasks - phpHow to find a variable value in a MOD expression? - mathhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1724166/how-can-i-force-the-repository-chooser-in-git-gui&usg=ALkJrhjo3xNfTG16UbM7dspdL1VQ05HoYQImplementing an article editing page in JSF - javaUsing a managed property with CommandButton in Java Server Faces - javaะัะธะฒัะทะบะธ MInefield ะฒ Xcode ะธ Interface Builder - xcodeList of iPhone apps that support openURL: - urlHow to use magnetic links in LibTorrent lib - c ++Navigation in JavaServer Faces, redirecting with the correct parameters - javaStream monitoring for scala members - multithreadingAll Articles