获取设备的本地时区时区、设备

由网友(老友提酒)分享简介:2010-06-14 02:21:49 + 0400或2010-06-14 02:21:49-0400 2010-06-14 02:21:49+0400 or 2010-06-14 02:21:49-0400是有办法根据与格式2010-06-14上午02时21 is there a way to convert...

2010-06-14 02:21:49 + 0400或2010-06-14 02:21:49-0400

2010-06-14 02:21:49+0400 or 2010-06-14 02:21:49-0400

是有办法根据与格式2010-06-14上午02时21

is there a way to convert this string to the date according to the local machine time zone with format 2010-06-14 02:21 AM

推荐答案

添加到什么@ org.life.java和@Erica说,这里是你应该做的。

Adding to what @org.life.java and @Erica said, here's what you should do

String dateStr = "2010-06-14 02:21:49-0400";
SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
TimeZone tz = TimeZone.getDefault();
sdf.setTimeZone(tz);
Date date = sdf.parse(dateStr);

sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String newDateStr = sdf.format(date);

System.out.println(newDateStr);

然后 newDateStr 将是您新的日期格式的字符串。

Then newDateStr will be your new date formatted string.

更新 @xydev,我给你的工作示例,请参见下面的完整源$ C ​​$ C:

UPDATE @xydev, the example I gave you works, see the full source code below:

/**
 * 
 */
package testcases;

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

/**
 * @author The Elite Gentleman
 *
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            String dateStr = "2010-06-14 02:21:49-0400";
            SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
            TimeZone tz = TimeZone.getDefault();
            sdf.setTimeZone(tz);
            Date date = sdf.parse(dateStr);

            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
            String newDateStr = sdf.format(date);

            System.out.println(newDateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

输出: 2010-06-14上午08点21分49秒

阅读全文

相关推荐

最新文章