How to convert time between timezone in Java, Primefaces? - The Koch Family
The Koch Family The Koch Family

Latest news

جاري التحميل ...

How to convert time between timezone in Java, Primefaces?


I use the calendar Primefaces component with timeOnly and timeZone attributes for using only hour format (HH:mm). Like this:
 <p:calendar id="xabsOvertimeTimeFrom" pattern="HH:mm" timeOnly="true" value="#{data.dateFrom}" timeZone="#{data.timeZone}"/>

We can convert the value of #{data.dateFrom} from GMT/UTC time zone to local, conversely, from local time zone to GMT/UTC time zone. Here is my functions:
package vn.nvanhuong.timezoneconverter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;



public class TimeZoneConverter {

/**
* convert a date with hour format (HH:mm) from local time zone to UTC time zone
*/
public static Date convertHourToUTCTimeZone(Date inputDate) throws ParseException {
if(inputDate == null){
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(inputDate);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
String dateString = ""+((hours>9)?""+hours:"0"+hours)+":"+((hours>9)?""+minutes:"0"+minutes)+"";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.parse(dateString);
}

/**
* convert a date with hour format (HH:mm) from UTC time zone to local time zone
*/
public static Date convertHourFromUTCToLocalTimeZone(Date inputDate) throws ParseException {
if(inputDate == null){
return null;
}
Date localFromGmt = new Date(inputDate.getTime() - TimeZone.getDefault().getOffset(inputDate.getTime()));

Calendar calendar = Calendar.getInstance();
calendar.setTime(localFromGmt);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
String dateString = ""+((hours>9)?""+hours:"0"+hours)+":"+((hours>9)?""+minutes:"0"+minutes)+"";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

return sdf.parse(dateString);
}

}

And Unit test for them:
package vn.nvanhuong.timezoneconverter.test;

import static org.junit.Assert.assertEquals;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.junit.Test;

import vn.nvanhuong.timezoneconverter.TimeZoneConverter;

public class TimeZoneConverterTest {

@Test
public void testConvertHourToUTCTimeZoneWhenNull() throws ParseException {
Date inputDate = null;
Date expected = null;
Date actual = TimeZoneConverter.convertHourToUTCTimeZone(inputDate);
assertEquals(expected,actual);
}

@Test
public void testConvertHourToUTCTimeZoneWhenNotNull() throws ParseException {
SimpleDateFormat isoFormat = new SimpleDateFormat("HH:mm");
Date inputDate = isoFormat.parse("19:21");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = isoFormat.parse("19:21");
Date actual = TimeZoneConverter.convertHourToUTCTimeZone(inputDate);
assertEquals(expected,actual);
}

@Test
public void testConvertHourToDefaultTimeZoneNull() throws ParseException {
Date inputDate = null;
Date expected = null;
Date actual = TimeZoneConverter.convertHourFromUTCToLocalTimeZone(inputDate);
assertEquals(expected,actual);
}

@Test
public void testConvertHourToDefaultTimeZoneNotNull() throws ParseException {
SimpleDateFormat isoFormat = new SimpleDateFormat("HH:mm");
Date expected = isoFormat.parse("09:21");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date inputDate = isoFormat.parse("09:21"); //16:21
Date actual = TimeZoneConverter.convertHourFromUTCToLocalTimeZone(inputDate);
assertEquals(expected, actual);
}

}

Hope that is useful for everyone! ;)

Comments



If you like the content of our blog, we hope to stay in constant communication, just enter your email to subscribe to the blog's express mail to receive new blog updates, and you can send a message by clicking on the button next ...

إتصل بنا

About the site

author The Koch Family <<  Welcome! I'm so glad that you stopped by Your Modern Family blog. Together, we will talk about raising kids, organizing the home and saving money! and Tips & tricks and more…

< Learn more ←

Blog stats

Sparkline 2513183

All Copyrights Reserved

The Koch Family

2020