Converting a UTC Date into a Date of different timezone

convert datetime to specific timezone c#
store datetime as utc and convert as per users time zone
java convert date from one timezone to another
java convert date to utc timezone
c# convert utc to specific timezone
utc time now
convert datetime to utc c#
convert timestamp from one timezone to another

I receive a date that represents a datetime in utc. Lets say: 21-Jun-2019 10:00

I'd like to convert this datetime to the timezone "Europe/Vienna" expecting: 21-Jun-2019 12:00

I do not understand, why my code below shows the same time for both

        Date utcFinish = new Date(new Date().getYear(), Calendar.JUNE, 21);
        TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
        Calendar finishInViennaTime = Calendar.getInstance(europeVienna);
        finishInViennaTime.setTime(utcFinish);

        System.out.println(format.format(utcFinish));
        System.out.println(format.format(finishInViennaTime.getTime()));

Output:

2019-06-21 00:00
2019-06-21 00:00

What would be the best java7 only (no joda, localdate pls) solution!? Thank you

EDIT: I also tried:

        SimpleDateFormat formatWithTimezone = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        formatWithTimezone.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));

        SimpleDateFormat formatonly = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        Date utcDate = new Date(new Date().getYear(), Calendar.JUNE, 21);

        System.out.println(formatonly.format(utcDate));
        System.out.println(formatWithTimezone.format(utcDate));

Output:

2019-06-21 00:00
2019-06-21 00:00

SOLUTION

Thanks for all the solutions. In the end the problem was the default timezone. Here is my current solution (further feedback welcome!):

        // Unfortunately this date has the wrong time zone (Local Time Zone),
        // because Date assumes Local Time Zone the database stores timestamps 
        // in utc that's why I now convert to a datestring and reparse

        Date finishTimeWrongTimeZone = new Date(new Date().getYear(), Calendar.JUNE, 21);
        // in reality i call the db here like getFinishTime();

        // get the plain date string without time shifting
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm");
        String dateWithoutTimeZone = formatter.format(finishTimeWrongTimeZone);

        // add the timezone to the formatter and reinterpret the datestring
        // effectively adding the correct time zone the date should be in
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        String finishTime = null;
        try {

            Date dateWithCorrectTimeZone = formatter.parse(dateWithoutTimeZone);

            // Convert to expected local time zone (europe/vienna)
            formatter.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
            finishTime = formatter.format(dateWithCorrectTimeZone);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(finishTime);

Alter the Timezone before doing the format. The date will be "converted" accordingly, but as we already told you, this old coding style has a lot of flaws:

public static void main(String[] x) {
    Date instant = new Date(new Date().getYear(), Calendar.JUNE, 21); // this call assumes the Timezone is your current default (system dependant).
    DateFormat sdf = SimpleDateFormat.getDateTimeInstance();

    sdf.setTimeZone(TimeZone.getTimeZone("Europe/Vienna"));
    System.out.println(sdf.format(instant)); //21 juin 2019 00:00:00

    sdf.setTimeZone(TimeZone.getTimeZone("Europe/Greenwich"));
    System.out.println(sdf.format(instant)); // 20 juin 2019 22:00:00

}

How to Convert Dates into Different Time Zones in Displayr, We have imported some data into Displayr but the date has been stored in UTC time. We want to change the below dates to our local Sydney time zone (AEST). I � Actually, when working date/time values within PowerApps, it would be converted into UTC Time zone value automatically. So when you put a date time value (e.g. Now()) for the start column in your Patch formula, the date/time value would be converted into a UTC time zone value firstly, then pass the converted UTC time zone value into your SQL Table.

I use one formatter object and changes the time zone on it

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm"); format.setTimeZone(TimeZone.getTimeZone("UTC"));

Date dUtc = format.parse("21-06-2019 10:00");
System.out.println(dUtc);

TimeZone europeVienna = TimeZone.getTimeZone("europe/vienna");

format.setTimeZone(europeVienna);
String sVienna = format.format(dUtc);
System.out.println(sVienna);

Java - Convert date and time between timezone, and Joda Time) to convert a date and time between different time zones. All examples will be converting the date and time from. (UTC+8:00)� The sys_extract_utc function gives you the UTC equivalent of your system time, but with on embedded time zone info - it's a plain timestamp, not a timestamp with time zone. So when you adjust it, it's implicitly converted to the system time zone with no adjustment, leaving you with the wrong actual time.

Modern (java.time) solution

I have a version for the old API below like you asked, but for completeness, I will also provide the more modern solution. I recommend you to look into ThreeTen-Backport if updating Java is not an option:

ZonedDateTime zdt = LocalDateTime.of(
    Year.now().getValue(), Month.JUNE, 21, 10, 0, 0
).atZone(ZoneOffset.UTC);

System.out.println(
    zdt.withZoneSameInstant(ZoneId.of("Europe/Vienna"))
      .format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"))
);
Old (java.util) solution

new Date(...) is deprecated, and you should not use it. If you really need to stick to the old API; you'll need to use Calendar:

Calendar utcFinish = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
utcFinish.set(Calendar.MONTH, Calendar.JUNE);
utcFinish.set(Calendar.DATE, 21);
utcFinish.set(Calendar.HOUR_OF_DAY, 10);
utcFinish.set(Calendar.MINUTE, 0);

And then use a DateFormat with the time zone that you actually wish to print it with:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
format.setTimeZone(europeVienna);

System.out.println(format.format(utcFinish.getTime()));
Output

Both solutions should output (at time of writing, in 2019):

2019-06-21 12:00

Converting times between time zones, Learn to convert times between from one time zone to another in . ConvertTimeToUtc method to convert the date and time from a specified� Select ClientTime column, navigate to Transform tab, choose data type and select Date/Time/TimeZone: After that, click close and apply. Till then we should have finished converting the time into Client local time. If any further questions, please feel free to post back.

Please use SimpleDateFormat to convert timezone

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date utcFinish = new Date(new Date().getYear(), Calendar.JUNE, 21);
TimeZone europeVienna = TimeZone.getTimeZone("Europe/Vienna");
format.setTimeZone(europeVienna);
System.out.println(format.format(utcFinish));

TimeZoneInfo.ConvertTimeFromUtc(DateTime, TimeZoneInfo , Converts a Coordinated Universal Time (UTC) to the time in a specified time zone. destinationTimeZone: TimeZoneInfo. The time zone to convert dateTime to. Date dateInAmerican = calendar.getTime()); In the above example, no matter what time zone you set in the Calendar, the Date object will be always printed with the default system time zone. (Check the Date.toString () source code) 3.2 The correct way should be using the DateFormat to format it :

Always Use UTC Dates And Times. As soon as you handle dates or , All other timezones can change: a country government can decide to to format it server side, you can just turn the date into a UTC ISO string,� SQL Server convert UTC to Local time conversion Using dateadd (), datediff (), getutcdate () and getdate () functions we can convert UTC DateTime to LOCAL time. DATEADD (): This function adds the value to the specified date part in the input DateTime.

Handling Local Timezones, UTC, Daylight Savings Time, and Leap , The examples are all stand-alone; in other words, you don't have to carry out all of them, you Where time zone has been added to the date as a UTC offset. Also notice that the datetime has been converted to FME format. Universal Time ( UTC ) to Your Local Time and Worldwide Time Conversions, Conversion Time Chart between Universal Time and Local Time

Datetime Data Types and Time Zone Support, You can replace the UTC offset with the TZR (time zone region) format The time zone offset is the difference (in hours and minutes) between local time and UTC (Coordinated UTC-8 has been changed to the local time zone, changing the hour from 2 to 3 Oracle Database performs all timestamp arithmetic in UTC time. Answer to "BigQuery converting to a different timezone" on Stackoverflow; Use cases. BigQuery displays data usually in UTC. That leads to problems when using date formatting functions because dates and times can be off. Converting the datetimes prior formatting into the correct timezone solves those issues. Common formats:

Comments
  • A date does not have a timezone. Conceptually, it is an instant in time (it only wraps a number of milliseconds after Unich Epoch), no matter where you are. Only when representing it as a String you attach a timezone, so it is the job of the DateFormat object to do the conversion.
  • @Shakka, the above spi's comment is exactly why your approach of "no joda or localdate" is faulty. These new ways of doing things exist precisely because legacy JDK date types are poorly named at the very least, and tricky to work with correctly.
  • side Note: Data class is outdated, poorly design and some methods are deprecated if possible use java.time api, ZonedDateTime class and your case getYear() is deprecated
  • Pretty sure that new Date(...) (which is deprecated btw) produces a time in local timezone, and not UTC.
  • @TiiJ7, as said above, java.util.Date is just a number of milliseconds since epoch, and as such doesn't really contain any time zone information. However, that specific constructor does indeed use the default system timezone to infer how to convert hour-minute values (0-0 for this case) into the epoch seconds value.
  • aah I see ... my test is wrong. new Date uses the local timezone... I will report back if this solves my problem!
  • thank you this works, the only problem I have is that I get a Date from the DB and if I call the calendar like so: utcFinish.setTime(myDate) -> I get the same behaviour again (meaning that I get the utc time)
  • Hmm, the problem is probably with how you retrieve the date from the database then. When I had to get dates from the database, I also had a problem like that. I had to use the getDate method that takes an extra calendar as input and pass it a calendar with UTC timezone. Not sure that'll fix it for you but it is worth a shot.
  • that's a good way to improve my new solution (see above). Thanks for your comments you led me to the devilish default timezone detail!
  • sorry this does not work for me, I get the same datetime!
  • in retrospect: this did not work, because new Date() constructor sets the default Locale as the timezone and I therefore get no change in the time