Java的SimpleDateFormat的类似于C# [英] Java SimpleDateFormat similar to C#

查看:2114
本文介绍了Java的SimpleDateFormat的类似于C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了今天的日期与此格式 {日期:2013-09-11T14:47:57.8895887 + 02:00}
这是因为我的JSON服务。研究了Windows Phone的和C#code。

我试着用这个方法:

 公共静态日期getTodayDate(){
    SimpleDateFormat的日期格式=新的SimpleDateFormat(
            YYYY-MM-dd'T'HH:MM:ss.SSSZ:Z);
    日期日期=新的日期();
    字符串dateString = dateFormat.format(日期);
    今天的日期= parseFromNormalStringToDate(dateString);
    今天返回;
}

但我得到这个回报率


  

2013-09-16T11:47:55.235 + 0200:+0200;


感谢您的帮助!


解决方案

有两件事情在这里改变。首先格式。

 的SimpleDateFormat DATEFORMAT =新的SimpleDateFormat(YYYY-MM-dd'T'HH:MM:ss.SSSSSSXXX); //这应该为你工作。虽然我必须说6S不这样做。你不会得到6 precisions毫秒。
日期日期=新的日期();
字符串dateString = dateFormat.format(日期); //你需要使用dateString为您JSON

和第二件事,格式化的日期是,你需要把你的JSON而不是解析回日期。但日期没有一个格式化选项。您只能获得您需要使用SDF格式的日期的字符串重新presentation。

例如: -

 公共静态无效的主要(字串[] args){
    SimpleDateFormat的日期格式=新的SimpleDateFormat(YYYY-MM-dd'T'HH:MM:ss.SSSSSSXXX);
    日期日期=新的日期();
    字符串dateString = dateFormat.format(日期); //你需要使用dateString为您JSON
    的System.out.println(dateString); //输出
}

和的输出,这是


  

2013-09-16T15:39:16.000257 + 05:30


以毫秒

6位precision是不可能的。如果您看到的Java 7自卫队的文档,你可以找到这样的: -

高亮显示的例子是你需要的,但与6毫秒precision,这是不可能的。因此,你可以用6 取值,但它只是实际的3毫秒的数字前加3前导零!这是唯一的解决方法可能在你的情况!

编辑: -

的SimpleDateFormat 的Andr​​oid不包含 X 。它提供了以Z 来代替。因此,新的格式字符串将是


  

YYYY-MM-dd'T'HH:MM:ss.SSSSSSZZZZZ


 的SimpleDateFormat DATEFORMAT =新的SimpleDateFormat(YYYY-MM-dd'T'HH:MM:ss.SSSSSSZZZZZ); //对于Android的

I have to get a today Date with this format {"Date":"2013-09-11T14:47:57.8895887+02:00"}. This is because my Json Service is studied for Windows Phone and C# code.

I tried with this method:

public static Date getTodayDate() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss.SSSZ:Z");
    Date date = new Date();
    String dateString = dateFormat.format(date);
    Date today = parseFromNormalStringToDate(dateString);
    return today;
}

but I get this return

2013-09-16T11:47:55.235+0200:+0200;

thanks for the help!

解决方案

There are 2 things to be changed here. First the format.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"); // This should work for you. Though I must say 6 "S" is not done. You won't get milliseconds for 6 precisions.
Date date = new Date();
String dateString = dateFormat.format(date); // You need to use "dateString" for your JSON

And the second thing, the formatted date is the which you need to put in your JSON and not parse it back to Date. But Date doesn't have a formatting option. You can only get a String representation of the Date in the format you need using SDF.

Ex:-

public static void main(String[] args) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
    Date date = new Date();
    String dateString = dateFormat.format(date); // You need to use "dateString" for your JSON
    System.out.println(dateString); // Output
}

and the output for this is

2013-09-16T15:39:16.000257+05:30

6 digit precision in milliseconds is not possible. If you see the docs of SDF in Java 7, you can find this:-

The highlighted example is the one you need, but with 6 milliseconds precision, which is not possible. Thus, you can use 6 S but it will just add 3 leading zeroes before the actual 3 millisecond digits! This is the only workaround possible in your case!

Edit:-

The SimpleDateFormat of Android does not contain X. It provides Z instead. Therefore your new format string will be

yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ"); // For Android

这篇关于Java的SimpleDateFormat的类似于C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆