如何在Android中将GMT转换为本地日期时间

时间:2016-10-13 18:53:19

标签: java android datetime

我有这样的时间:2016年10月27日星期四18:17:47 GMT

我已经尝试了以下代码,但没有工作

<a class="fancyboxgallery" rel="book" href="http://www.booclin.ovh/tom/2/index/photos/projet/6.jpg" title="">
<img class="fancyboxthumbnailsgallery" src="http://www.booclin.ovh/tom/2/index/photos/projet/06.jpg" alt=""/>
</a>

<a class="fancyboxgallery" rel="book" href="http://www.booclin.ovh/tom/2/index/photos/projet/5.jpg" title="">
<img class="fancyboxthumbnailsgallery" src="http://www.booclin.ovh/tom/2/index/photos/projet/05.jpg" alt=""/>
</a>

<a class="fancyboxgallery" rel="book" href="http://www.booclin.ovh/tom/2/index/photos/projet/4.jpg" title="">
<img class="fancyboxthumbnailsgallery" src="http://www.booclin.ovh/tom/2/index/photos/projet/04.jpg" alt=""/>
</a>

<a class="fancyboxgallery" rel="book" href="http://www.booclin.ovh/tom/2/index/photos/projet/3.jpg" title="">
<img class="fancyboxthumbnailsgallery" src="http://www.booclin.ovh/tom/2/index/photos/projet/03.jpg" alt=""/>
</a>

<a class="fancyboxgallery" rel="book" href="http://www.booclin.ovh/tom/2/index/photos/projet/2.jpg" title="">
<img class="fancyboxthumbnailsgallery" src="http://www.booclin.ovh/tom/2/index/photos/projet/02.jpg" alt=""/>
</a>


<a class="fancyboxgallery" rel="book" href="http://www.booclin.ovh/tom/2/index/photos/projet/1.jpg" title="">
<img class="fancyboxthumbnailsgallery" src="http://www.booclin.ovh/tom/2/index/photos/projet/01.jpg" alt=""/>
</a>

2 个答案:

答案 0 :(得分:1)

您的输入日期和输入格式互不匹配。请尝试使用此输入格式:

SimpleDateFormat inputFormat = new SimpleDateFormat
    ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);

答案 1 :(得分:0)

以下是一些功能:

public static Date toLocale(Date date) {
    int GMTOffset = (int) TimeUnit.HOURS.convert(calendar.getTimeZone().getRawOffset(),
            TimeUnit.MILLISECONDS);
    return addHours(date, GMTOffset);
}


public static Date addHours(Date date, int hours) {
    calendar.setTime(date);
    calendar.add(Calendar.HOUR_OF_DAY, hours);
    return calendar.getTime();
}