converting long string to date

前端 未结 6 789
盖世英雄少女心
盖世英雄少女心 2020-12-20 02:40

I am getting date value from DB as a long value. I am converting this to string to use parse function. Given below is my code

6条回答
  •  半阙折子戏
    2020-12-20 03:18

    You can try following code:

    private Date getGMTDate(long date) {
        SimpleDateFormat dateFormatGmt = new SimpleDateFormat(
                "yyyy-MMM-dd HH:mm:ss");
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        SimpleDateFormat dateFormatLocal = new SimpleDateFormat(
                "yyyy-MMM-dd HH:mm:ss");
    
        Date temp = new Date(date);
    
        try {
            return dateFormatLocal.parse(dateFormatGmt.format(temp));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return temp;
    }  
    

    I hope this will help you.

提交回复
热议问题