How do I parse a standard date into GMT?

可紊 提交于 2019-12-02 11:01:06

问题


Can someone show me a piece of java code that parses this date:

2009-08-05

INTO THIS GMT DATE:

2009/217:00:00

====

what i have so far is:

       java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");

       java.util.Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT"));
       format.setCalendar(cal);
       java.util.Date date = format.parse(sdate);

but its not working


回答1:


Here is the format you're looking for:

Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2009-08-05");
String parsedDate = new SimpleDateFormat("yyyy/D:HH:mm").format(date);



回答2:


format.setTimeZone(TimeZone.getTimeZone("GMT"));

That's how to set it to GMT at least. Not sure where you are getting 2009/217 from 2009-08-05




回答3:


SimpleDateFormat dateFormatGmt = new SimpleDateFormat("dd:MM:yyyy HH:mm:ss");
    dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(dateFormatGmt.format(new Date())+"");

This will convert your local time to GMT.



来源:https://stackoverflow.com/questions/1257542/how-do-i-parse-a-standard-date-into-gmt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!