[英]how to convert given offset time(in seconds) to Format like +hh:mm in java?
我想提供時區支持。為此,我將日期值與UTC時間一起存儲在數據庫中,然后轉換時間取決於用戶本地時區。為此,我在數據庫查詢中使用了這一行
CONVERT_TZ(modifiedtime,'+00:00','+05:30')
這將為我提供印度標准時間的本地時區...但是我的問題是我僅針對特定用戶偏移了econd和ther id ..
所以無論如何我可以將偏移量轉換為+05:30 or say +04.00 ,+04.30..
任何人都可以給我適當的解決方案,如果我可以將以秒為單位的偏移量轉換為+hh:mm
所以我可以直接給它查詢...
我在使用liferay 6.1門戶是因為我已經創建了我的自定義portlet..so,因此我需要用Java語言編寫此代碼...所以,請問有人可以指導我嗎?
以下內容可能會更簡短,更方便
private static SimpleDateFormat plus = new SimpleDateFormat("+hh:mm");
private static SimpleDateFormat minus = new SimpleDateFormat("-hh:mm");
private static String getTimeCode(int seconds) {
SimpleDateFormat simpleDateFormat = (seconds < 0) ? minus : plus;
if (seconds < 0) seconds = 12 * 3600 - seconds;
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat.format(new Date(seconds * 1000));
}
int seconds = 5 * 3600 + 30 * 60;
String format = getTime(seconds); //+05:30
format = getTime(-1 * seconds); //-05:30
上面的代碼可能並不適用所有編碼標准,但是SO的縮寫
您可以嘗試編寫自己的轉化代碼或嘗試執行以下操作:
long offset = 19800; //offset IST (seconds)
long time = TimeUnit.SECONDS.toMinutes(offset); //or offset/60
long hour = time / 60;
long min = Math.abs(time % 60);
String hrStr = "";
if (hour > 0 && hour < 10) {
hrStr = "+0" + String.valueOf(hour);
} else if (hour >= 10) {
hrStr = "+" + String.valueOf(hour);
} else if (hour < 0 && hour > -10) {
hrStr = "-0" + String.valueOf(hour).substring(1);
} else {
hrStr = String.valueOf(hour);
}
String minStr = String.valueOf(min);
if (min < 10) {
minStr = "0" + (time % 60);
}
String timeStr = hrStr + ":" + minStr;
System.out.println(timeStr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.