1

I tried the following code that should allow me to convert the time pass as parameter to a string with one hour more (GMT+1).
But I always get the same time I have in my object Time

public static String getFormattedTimeLabel(final Time time) {
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(time.getTime());
    SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
    sdf.setCalendar(cal);
    sdf.setTimeZone(TimeZone.getDefault());
    return sdf.format(cal.getTime());
}    

Does anybody know how to solve it?

EDIT : Ok so finally I created the following function in my controller.
The timezone I set in the Simple date format is the timezone of the client recover thanks to HttpServletRequest.
You can see the printed result of System.out after the code.

private Time getTimeLocal(Time requestTime) {
     Calendar cal = new GregorianCalendar();
     cal.setTimeInMillis(requestTime.getTime());
     SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
     sdf.setCalendar(cal);
     System.out.println(requestTime);
     sdf.setTimeZone(tz);
     System.out.println(tz.getID());
     String dt = sdf.format(cal.getTime());
     Date date = null;
    try {
        date = sdf.parse(dt);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(dt);
     return new Time(date.getTime());
}

10:00:00 //the Time object
Europe/Paris  // The id of the client timezone (GMT+1 for me)
10 : 00 //the date. the time printed is not correct it should be set with the timezone of the client so for me GMT+1 so 11:00:00
Jerome Campeaux
  • 353
  • 2
  • 9
  • 19
  • @BoristheSpider TimeZone.getDefault() should be the Timezone of the system. and I don't use the one I want because it can change depending on the country where the application is used – Jerome Campeaux Jan 04 '16 at 10:52
  • Have a look to this thread http://stackoverflow.com/questions/3581258/adding-n-hours-to-a-date-in-java – Aman Jain Jan 04 '16 at 10:54
  • 1
    @Spidi'sWeb: the question is not about adding hours, it's just about setting the timezone and formatting. – Remigius Stalder Jan 04 '16 at 10:56
  • @BoristheSpider The time I get come from my database, i didn't printed it but normally if i format it with Timezone.getDefault() I should have this time + 1 hour cause I'm in Paris. The problem is the hour is not added at the end – Jerome Campeaux Jan 04 '16 at 11:02
  • 1
    @JeromeCampeaux I see - databases make things more complicated. How is the data stored in the database and what timezone is the database set to? What is the timezone of the JDBC connection to the database? I ask because JDBC has the nasty habit of doing automatic conversion... I would suggest that you print the data _as you receive it_ to see what sort of munging JDBC has inflicted on you. – Boris the Spider Jan 04 '16 at 11:31
  • @BoristheSpider I get the data from the form with the client timezone, then i convert it to GMT time and it's correctly saved in database. after that I want to convert the time of the client again – Jerome Campeaux Jan 04 '16 at 11:35
  • @dur ok but the problem is that the default timezone can change, it's not always GMT+1. it depends on the timezone of the client. got by HttpServletRequest – Jerome Campeaux Jan 04 '16 at 14:07
  • The `Time` class is derived from `Date`, which is basically just a wrapper for a `long` that denotes the number of milliseconds passed since standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT and is therefore timezone independent. The methods `toString()` renders the object in the default TZ, in addition to this there are `toLocaleString()` and `toGMTString()`. To specify a particular TZ, you can either use a Calendar or use the TZ only for rendering. For more control over times and dates, you might want to use Joda Time - see http://www.joda.org/joda-time/ – Remigius Stalder Jan 04 '16 at 14:12

2 Answers2

3

The code seems to work correctly. When you output the result, the time is displayed as the time in the correct timezone. Trying to add a timezone parameter as follows:

public static String getFormattedTimeLabel(final Time time, String tzId) {
  Calendar cal = new GregorianCalendar();
  cal.setTimeInMillis(time.getTime());
  SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
  sdf.setCalendar(cal);
  sdf.setTimeZone(tzId == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tzId));
  return sdf.format(cal.getTime());
}

and invoking it as:

Time time = new Time(System.currentTimeMillis());
System.out.println(getFormattedTimeLabel(time, null));
System.out.println(getFormattedTimeLabel(time, "UTC"));

gives the following results (as of now):

12 : 08
11 : 08

As I am in TZ GMT+1 and now it's short past noon, the results are correct.

BTW, printing the time object like this:

System.out.println(time.toString());

gives:

12:08:16

i.e. it is formatted by default in the default timezone.

BTW again, the function could be simplified not to use a Calendar object as follows:

public static String getFormattedTimeLabel(final Time time, String tzId) {
  SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
  sdf.setTimeZone(tzId == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tzId));
  return sdf.format(time);
}

Setting the timezone in the SimpleDateFormat is sufficient.

dur
  • 15,689
  • 25
  • 79
  • 125
Remigius Stalder
  • 1,921
  • 2
  • 26
  • 31
0

ok I finally find the solution thanks to this post Date TimeZone conversion in java? and the help of all the guys took part of my post.

My final code is :

private Time getTimeLocal(Time requestTime) {
    String ret = Utils.getFormattedTimeLabel(requestTime);
    SimpleDateFormat sdfgmt = new SimpleDateFormat("HH : mm");
    sdfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));

    SimpleDateFormat sdfmad = new SimpleDateFormat("HH : mm");
    sdfmad.setTimeZone(TimeZone.getTimeZone(tz.getID()));

    Date inptdate = null;
    try {
        inptdate = sdfgmt.parse(ret);
    } catch (ParseException e) {e.printStackTrace();}

    String localString = sdfmad.format(inptdate);
    try {
        inptdate = sdfmad.parse(localString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return new Time(inptdate.getTime());
}
Community
  • 1
  • 1
Jerome Campeaux
  • 353
  • 2
  • 9
  • 19
  • `Time` is, as far as I am aware, milliseconds since the epoch in GMT - it cannot _be_ "local". This code is a little odd, as what you are doing is essentially adding or subtracting your timezone offset from a value that should always be milliseconds since the epoch GMT... – Boris the Spider Jan 05 '16 at 01:15