Domanda

Ho un oggetto calendario [location locale] che è in EST: dire 6 novembre, 15:34 ... e ho impostato il fuso orario su GMT+5: 30 ... ora quando faccio il Calendar.HOUR_OF_DAY Mi restituisce 2 ... che so è perfetto .. poiché sono 15:34 + 5 ore a GMT e poi +5: 30 al fuso orario .. che significa solo .. 26:04 che è 2 del 7 °.

Tuttavia, la data rimane ancora il 6 novembre ... e localDate.getTime() Ritorna ancora il 6 novembre .. e anche quando stampano la data locale .. Mostra il fuso orario come +5: 30, ma il giorno e tutto il resto sono ancora come ora locale originale .. [cioè 6 novembre

Non sono semplicemente in grado di capire perché ...

Modificare ::

Quindi capisco che non ho bisogno di modificare la data insieme al fuso orario. Cambia solo la data che viene visualizzata adatta a quella posizione e che può essere fatto usando il fuso orario che è stato impostato.

Nessuna soluzione corretta

Altri suggerimenti

localDate.getTime() Restituisce a java.util.Date che è una quantità di tempo grezzo da un punto fisso. Gli zone dei tempi influenzano solo la rappresentazione leggibile umana del momento.

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

sono entrambi gli stessi punti nel tempo assoluto. Sono solo due diversi modi umani di descrivere lo stesso istante.

Quindi cambiare il fuso orario sul calendario non ha alcun effetto sul valore restituito da getTime()

Date Gli oggetti non hanno un fuso orario - a Date L'oggetto rappresenta un momento "assoluto" nel tempo. Quando stampano un Date oggetto (chiamando implicitamente o esplicitamente toString() su di esso):

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

Quindi verrà formattato usando un formato predefinito che mostrerà la data nel tuo fuso orario locale, non importa se hai il Date oggetto da a Calendar che era impostato su un fuso orario diverso.

Se vuoi visualizzare il Date In un fuso orario diverso, usa un file DateFormat oggetto e imposta il fuso orario in cui si desidera visualizzare la tua data su quell'oggetto:

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 domanda non è chiara

La tua domanda è confusa.

Potresti essere confuso sul significato dei fusi orari. Come dicevano le risposte corrette di Jesper e Affe, muovere i fusi orari non cambia il punto sulla linea temporale dell'universo. Supponiamo che Bob a New York Stati Uniti chiami Susan a Reykjavík Islanda. L'Islanda utilizza UTC come fuso orario tutto l'anno. Bob e Susan si parlano nello stesso momento. Ma se Bob guarda l'orologio sul muro, vede una volta visualizzata 5 ore prima di un orologio sul muro di Susan. New York ha un offset di cinque ore dietro UTC (-5: 00).

Un altro problema con la tua domanda: parli anche di un fuso orario delle 5:00 e di un offset 5:30. Cos'è questo? O hai in mente due fusi orari e GMT/UTC?

Joda-time

Prenderò una pugnalata nel darti un po 'di codice sorgente di esempio.

Il Joda-time La biblioteca semplifica il lavoro del tempo.

// © 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 );

Quando corri ...

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

A proposito di Joda-time ...

// 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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top