Pregunta

Tengo un objeto de calendario [localdate] que está en est: digamos 6 de noviembre, 15:34 ... y establecí la zona horaria en GMT+5: 30 ... ahora cuando hago el Calendar.HOUR_OF_DAY Me devuelve 2 ... lo cual sé que es perfecto ... ya que es 15:34 + 5 horas a GMT y luego +5: 30 a la zona horaria ... lo que solo significa ... 26:04 que es 2 de 7º.

Sin embargo, la fecha aún se queda como el 6 de noviembre ... y localDate.getTime() todavía regresa el 6 de noviembre ... e incluso cuando imprimo la fecha local ... muestra la zona horaria como +5: 30, pero el día y todo lo demás sigue siendo la hora local original ... [es decir, 6 de noviembre

Simplemente no puedo entender por qué ...

Editar ::

Por lo tanto, entiendo que no necesito cambiar la fecha junto con la zona horaria. Solo cambia la forma en que la fecha se muestra adecuada para esa ubicación y que se puede hacer utilizando la zona horaria que se ha establecido.

No hay solución correcta

Otros consejos

localDate.getTime() Devuelve un java.util.Date que es una cantidad de tiempo bruto desde un punto fijo. Las zonas horarias solo afectan la representación legible humana del punto en el tiempo.

15:34 Nov 6th UTC - 5y02:04 Nov 7th UTC + 5:30

Ambos son exactamente el mismo punto en tiempo absoluto. Son solo dos formas humanas diferentes de describir el mismo instante.

Por lo tanto, cambiar la zona horaria en el calendario no tiene ningún efecto sobre el valor devuelto por getTime()

Date Los objetos no tienen una zona horaria: un Date El objeto representa un momento "absoluto" en el tiempo. Cuando imprime un Date objeto (llamando implícita o explícitamente toString() en eso):

Date date = ...;
System.out.println(date);

entonces se formateará utilizando un formato predeterminado que mostrará la fecha en su zona horaria local, no importa si tiene el Date objeto de un Calendar que se estableció en una zona horaria diferente.

Si quieres mostrar el Date En una zona horaria diferente, use un DateFormat objeto y configure la zona horaria en la que desea mostrar su fecha en ese objeto:

Date date = ...;

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
df.setTimeZone(TimeZone.getTimeZone("UTC"));  // For example, UTC

// Prints 'date' in UTC
System.out.println(df.format(date));

La pregunta no está clara

Tu pregunta es confusa.

Puede estar confundido sobre el significado de las zonas horarias. Como dijeron las respuestas correctas de Jesper y Affe, las zonas horarias cambiantes no cambian el punto en la línea de tiempo del universo. Supongamos que Bob en Nueva York, Estados Unidos, llama a Susan en Reykjavík Islandia. Islandia usa UTC como su zona horaria durante todo el año. Bob y Susan se están hablando en el mismo momento en el tiempo. Pero si Bob mira el reloj en su pared, ve un momento que se muestra 5 horas antes que un reloj en la pared de Susan. Nueva York tiene una compensación de cinco horas detrás de UTC (-5: 00).

Otro problema con su pregunta: también hablas de un desplazamiento de la zona horaria de 5:00, así como una compensación de 5:30. ¿Cuál es? ¿O tienes dos zonas horarias en mente y GMT/UTC?

Joda-tiempo

Tomaré una puñalada para darle un poco de código fuente de ejemplo.

los Joda-tiempo La biblioteca facilita el trabajo de la fecha.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// Use time zone names rather than explicit number of hours offset is generally a good thing.
// Affords Joda-Time an opportunity to make adjustments such as Daylight Saving Time (DST).

// Question asked:
// (1) Start with a US east coast time (Standard offset of -5:00) of November 6, 2013 15:34.
// (2) Move that datetime to UTC (GMT) time zone (no offset).
// (3) Move that datetime to Kolkata (formerly known as Calcutta) India time zone (Standard offset of +05:30).

// Joda-Time has deprecated use of 3-letter time zone codes because of their inconsistency. Use other identifier for zone.
// Time Zone list: http://joda-time.sourceforge.net/timezones.html  (Possibly out-dated, read note on that page)
org.joda.time.DateTimeZone newyorkTimeZone = org.joda.time.DateTimeZone.forID( "America/New_York" );
org.joda.time.DateTimeZone kolkataTimeZone = org.joda.time.DateTimeZone.forID( "Asia/Kolkata" );

// Question calls for: EST Nov 6, 15:34 (Standard offset of -5:00).
// This DateTime constructor calls for passing: year, month, day, time zone.
org.joda.time.DateTime dateTimeInNewYork = new org.joda.time.DateTime( 2013, org.joda.time.DateTimeConstants.NOVEMBER, 6, 15, 34, newyorkTimeZone );
// Move to UTC time zone (no offset).
org.joda.time.DateTime dateTimeUtc = dateTimeInNewYork.toDateTime( org.joda.time.DateTimeZone.UTC );
// Move to Kolkata IN time zone (Standard offlet of +05:30).
org.joda.time.DateTime dateTimeInKolkata = dateTimeUtc.toDateTime( kolkataTimeZone ); // Or invoke this method on dateTimeInNewYork, does not matter which.

// All three of these date-time objects represent the same moment in the time-line of the Universe,
// but present themselves with different time-zone offsets.
System.out.println( "dateTimeInNewYork: " + dateTimeInNewYork );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeInKolkata: " + dateTimeInKolkata );

Cuando corre ...

dateTimeInNewYork: 2013-11-06T15:34:00.000-05:00
dateTimeUtc: 2013-11-06T20:34:00.000Z
dateTimeInKolkata: 2013-11-07T02:04:00.000+05:30

enter image description here

Sobre el tiempo de joda ...

// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
// http://www.joda.org/joda-time/

// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310

// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601

// About Daylight Saving Time (DST): https://en.wikipedia.org/wiki/Daylight_saving_time

// Time Zone list: http://joda-time.sourceforge.net/timezones.html
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top