skcrpk
skcrpk

Reputation: 556

How to add user timezone to utc date

how to add user timezone to utc i am getting utc date like this

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm:ss");
DateTime dateTime = formatter.withOffsetParsed().parseDateTime(getval[2]);
DateTime dateTimeUtc = dateTime.toDateTime(DateTimeZone.UTC);

Now i want to get user Timezone and add it to utc to convert that to localtime

UPDATE

i was able to get the user timezone but could add it to the utc

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-ddHH:mm:ss");
DateTime dateTime = formatter.withOffsetParsed().parseDateTime(getval[2]);

java.util.Calendar now = java.util.Calendar.getInstance();
java.util.TimeZone timeZone = now.getTimeZone();

DateTimeZone dtZone = DateTimeZone.forID(timeZone.getID());
DateTime dateTimeUtc = dateTime.toDateTime(DateTimeZone.UTC);
ofm.setDate(dateTimeUtc.toDateTime(dtZone).toDate());

Upvotes: 2

Views: 1098

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 340350

java.time

The Joda-Time project was succeeded by the java.time framework defined in JSR 310. Here is the modern solution using those new classes found in Java 8 and later.

Your input format is nearly compliant with the ISO 8601 standard. The data is just missing the T between the date portion and the time-of-day portion, and is missing a Z on the end to indicate UTC. See if you can educate the publisher of your data about this important standard.

String input = "2019-01-23T01:23:45.123456789Z" ;

The java.time classes use the standard formats by default. So no need to specify a formatting pattern.

Instant instant = Instant.parse( input ) ;

instant.toString() = 2019-01-23T01:23:45.123456789Z

If you can get the input format changed, define a formatting pattern to match.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-ddHH:mm:ss" ) ;

Lacking any indicator of time zone or offset, we must parse as a LocalDateTime. Note that such an object does not represent a moment, is not a specific point on the timeline.

String input = "2019-01-2301:23:45" ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;

ldt.toString() = 2019-01-23T01:23:45

You claim to be sure this date and time were intended to represent a moment in UTC. So we can apply an offset using the constant ZoneOffset.UTC to produce a OffsetDateTime.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

odt.toString() = 2019-01-23T01:23:45Z

Then you said you want to adjust this into a specific time zone. Same moment, same point on the timeline, but different wall-clock time.

Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

zdt.toString() = 2019-01-23T02:23:45+01:00[Africa/Tunis]

As you can see, Tunisia on that date was running an hour ahead of UTC. So the time-of-day appears to be 2 AM rather than 1 AM.

Upvotes: 1

user4883259
user4883259

Reputation:

Here's a small example that gets the difference from a list of time zones (in hours):

    import java.util.Date;
    import java.util.TimeZone;
    public class StackOverflowTimeZone {
        public static void main(String[] a) {
            Date date = new Date();
            for(int index = 0; index < TimeZone.getAvailableIDs().length; index++) {
                System.out.println(TimeZone.getAvailableIDs()[index] + " offset from UTC: " + TimeZone.getTimeZone(TimeZone.getAvailableIDs()[index]).getOffset(date.getTime()) / (60 * 60 * 1000) + " hours.");
            }
        }
    }

The abstract class TimeZone was designed to get the offset of a designated time zone from Coordinated Universal Time (UTC). There is a list of time zones that can be found by using the method TimeZone.getAvailableIDs(). After getting the offset, you will need to do a few small calculuations in order to find out whether the designated time zone is ahead or behind UTC. The sign (+/-) of your output should correlate to whether that designated time zone is ahead or behind UTC.

Upvotes: 0

Dillibabu kumar
Dillibabu kumar

Reputation: 28

This below code may help you to get the time zone of the user

//get Calendar instance
Calendar now = Calendar.getInstance();

//get current TimeZone using getTimeZone method of Calendar class
TimeZone timeZone = now.getTimeZone();

//display current TimeZone using getDisplayName() method of TimeZone class
System.out.println("Current TimeZone is : " + timeZone.getDisplayName());

also the below link helps you to convert user's timezone to UTC

link

Upvotes: 1

Related Questions