-1

I have a date for example Tue Jun 08 19:00:00 2021

And I want to indicate that this date is in the time zone GMT+3. Without changing it, it is already in GMT+3. Because I make a configuration at the backoffice level and I retrieve this date in the front according to the timezone of each user.

I have entered Tue Jun 08 19:00:00 2021 in the date field and after saving the model the value is getting saved as Tue Jun 08 20:00:00 WEST 2021 in the database. The server is in Africa/Casablanca timezone. Please explain how this conversion is happening !

I need to save the date as the user chose it. Tue Jun 08 19:00:00 WEST 2021

Rodik
  • 271
  • 2
  • 19
  • If you show us the code where the conversion is happening, we may have a chance. – Ole V.V. Jun 23 '21 at 03:37
  • I recommend you don’t use `java.util.Date`. That class is poorly designed and long outdated. Instead use either `ZonedDateTime` or `OffsetDateTime`; both are from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 23 '21 at 03:40
  • 1
    What do you mean that it is already in GMT+3? As far as I know, WEST is for Western European Summer Time, agrees with Africa/Casablanca and is at GMT+1, not +3. Or does another interpretation of the abbreviation *WEST* exist? – Ole V.V. Jun 23 '21 at 03:43
  • Does this answer your question? [SimpleDateFormat returns wrong time zone during parse](https://stackoverflow.com/questions/16107898/simpledateformat-returns-wrong-time-zone-during-parse) – Ole V.V. Jun 23 '21 at 03:45
  • Related and overlapping: [Java / SAP HYBRIS : Timezone Date Issues in Java/hybris Backoffice](https://stackoverflow.com/questions/68088602/java-sap-hybris-timezone-date-issues-in-java-hybris-backoffice) – Ole V.V. Jun 23 '21 at 03:50
  • If the configuration is under your control, you should be communicating date-time values textually using ISO 8601 formats only rather than the format seen here. – Basil Bourque Jun 23 '21 at 07:47
  • In the database are you saving text or using a date-time type? What database? What data type? Show code example. Voting to close for lack of details. – Basil Bourque Jun 23 '21 at 07:48
  • @BasilBourque in data base i'm saving java.util.Date type. for my needs I have entry field date and a list of times zones I format the date to have just the time HH: mm. suddenly, when the user chooses a date and a time zone, I want to save it in the database with this time zone example the user chosen for example 4:00 PM and time zone GMT + 2 the value I want to save is 4:00 PM in GMT + 2 – Rodik Jun 23 '21 at 08:58
  • 1
    @Rodik You’ve still not answered my request for more details. You make it very difficult for us to help you. And your Question is likely a duplicate. – Basil Bourque Jun 23 '21 at 17:12

1 Answers1

0

I'm not so sure if this would help, but here is some code that takes the system time and displays it in h:m:s format for GMT +3. You could likely modify it easily to your formatting:

import java.lang.*; 

class Main {   public static void main(String[] args) {
    long ms = System.currentTimeMillis();
    long sec = ms / 1000;
    long min = sec / 60;
    long hrs = (min / 60);
    long actualHrs = hrs % 24;
    long actualMin = min % 60;
    long actualSec = sec % 60;
    long ESTHrs = actualHrs - 5;
    if(actualHrs <= 12 && actualHrs > 0){
      System.out.println("The Current Time (GMT) is " + actualHrs + ":" + actualMin + ":" + actualSec + " AM"); 
    }
    else if(actualHrs > 12){
      System.out.println("The Current Time (GMT) is " + (actualHrs - 12) + ":" + actualMin + ":" + actualSec + " PM"); 
    }

If you could also reply with some of your code, maybe I could more easily help identify the problem.

Tarran M
  • 1
  • 1
  • 1
    I recommend we don’t do our own time math. The library does that a lot better. For the current time in GMT+3 use `OffsetDateTime.now(ZoneOffset.ofHours(3))`. And use a `DateTimeFormatter` to display it in 12 hour format with AM or PM marker. – Ole V.V. Jun 23 '21 at 03:56