如何在Java中将2012-01-01T08:44:36 + 01:00转换为datetime(yyyy-MM-dd hh:mm:ss)?

时间:2012-02-06 08:38:34

标签: java

有人可以告诉我如何在Java中将此字符串2012-01-01T08:44:36+01:00转换为2012-01-01 08:44:36吗?

由于

4 个答案:

答案 0 :(得分:0)

使用SimpleDateFormat课程格式化您的日期。

String value = "2012-01-01T08:44:36+01:00";
String date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try
{
    Date d = format.parse(value);
    date = format.format(d);
}
catch (ParseException ex)
{
    System.out.println(ex);
}
return date

有关http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

的更多信息

答案 1 :(得分:0)

但请记住,SimpleDateFormat不是线程安全的。 你可以从apache使用FastDateFormat: FastDateFormat

用法:

// create a formatter that simply prints the year and month
FastDateFormat formatter =
    new FastDateFormat( "yyyy-MM",
                         TimeZone.getDefault( ),
                         Locale.getDefault( ) );
// output equals "2012-02"
String output = formatter.format( new Date( ) );

解析日期:

FastDateFormat formatter = FastDateFormat.getInstance("yyyy-MM");
String s = "2012-01-01T08:44:36+01:00";
try {
    Object o = DateUtils.parseDate(s, new String[]{"yyyy-MM-dd'T'HH:mm:ss+01:00"});
    // output equals "2012-01"
    String output = formatter.format(o);
} catch (ParseException e) {
    System.err.println("Bad date: " + s);
}

答案 2 :(得分:0)

有时最简单的是最好的

String s = "2012-01-01T08:44:36+01:00";
// s2 = "2012-01-01 08:44:36"
String s2 = s.substring(0,10) + ' ' + s.substring(11, 19);

答案 3 :(得分:0)

  

有时最简单的是最好的

String s = "2012-01-01T08:44:36+01:00";
// s2 = "2012-01-01 08:44:36"
String s2 = s.substring(0,10) + ' ' + s.substring(12, 8);

这种方式可能会产生带索引的错误,可能会出现,例如:-) 如果遇到一些错误的字符串,你会在找到错误时花费大量时间,因为这种简单的方法没有对输入字符串进行任何检查。

正确的索引:

String s2 = s.substring(0,10) + ' ' + s.substring(11, 19);

如果你想使用日期和字符串一起使用,我更喜欢使用RegXP:

    // You can parse your xml output using regXp:
    String s = "<root><date>2012-01-01T08:44:36+01:00</date></root>";

    Pattern pattern = Pattern.compile("(.+)(T)(.+)([+-])");
    Matcher matcher = pattern.matcher(s);

    if (matcher.find()) {
        System.out.println(matcher.group(1) + ' ' + matcher.group(3));
    } else {
        System.err.println("Bad date: " + s);
    }

速度测试:

public class tempClass {

    static final String s = "2012-01-01T08:44:36+01:00".intern();
    static String formattedDate = null;
    static final int loop = 5 * 1000;
    static final String[] patterns = new String[]{"yyyy-MM-dd'T'HH:mm:ss+01:00".intern()};

    public static void main(String[] args) throws ParseException, InterruptedException {
        testSimpleWay();
        testRegXp();
        testSimpleDateFormat();
        testFastDateFormat();
    }

   private static void testRegXp() {
        Pattern pattern = Pattern.compile("(.+)(T)(.+)([+-])");
        Matcher matcher;

        final long timeStamp = System.currentTimeMillis();            
        for (int i = 0; i < loop; i++) {
            matcher = pattern.matcher(s);
            if (matcher.find()) {
                formattedDate = matcher.group(1) + ' ' + matcher.group(3);
            } else {
                System.err.println("Bad date: " + s);
            }
        }
        System.out.println("Duration of RegXP: " + (System.currentTimeMillis() - timeStamp) + " ms.");
    }

    private static void testSimpleWay() {
        long timeStamp = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            formattedDate = s.substring(0, 10) + ' ' + s.substring(11, 19);
        }
        System.out.println("Duration of Simple way: " + (System.currentTimeMillis() - timeStamp) + " ms.");
    }

    private static void testSimpleDateFormat() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        final long timeStamp = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            try {
                formattedDate = simpleDateFormat.format((Date) DateUtils.parseDate(s, patterns));
            } catch (ParseException ex) {
                System.err.println("Bad date: " + s);
            }
        }
        System.out.println("Duration of SimpleDateFormat: " + (System.currentTimeMillis() - timeStamp) + " ms.");
    }

    private static void testFastDateFormat() {
        FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

        final long timeStamp = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            try {
                formattedDate = fastDateFormat.format((Date) DateUtils.parseDate(s, patterns));
            } catch (ParseException e) {
                System.err.println("Bad date: " + s);
            }
        }
        System.out.println("Duration of FastDateFormat: " + (System.currentTimeMillis() - timeStamp) + " ms.");
    }

<强>输出:

Duration of Simple way: 5 ms.
Duration of RegXP: 20 ms.
Duration of SimpleDateFormat: 114 ms.
Duration of FastDateFormat: 65 ms.