Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * Convert Date to String in HK TimeZone
     * @param date
     * @param format Format of the output string (e.g. "yyyy-MM-dd HH:mm:ss", "HH:mm:ss")
     * @return output string
     */
    public static String convertDateToStr(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.ENGLISH);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0000"));
        String dateStr = "";
        if (date != null) {
            dateStr = dateFormat.format(date);
        } else {
            dateStr = dateFormat.format(new Date());
        }
        return dateStr;
    }
}