简体   繁体   English

如何在Java中将datetime转换为String?

[英]How to convert datetime to String in java?

while working on facebook api I encountered a field called start_time is of type datetime, i declared a start_time field as a string in my DTO class, while i was converting json to java i could able to get the start_time and i populated in my dto object but when i tried retrievng from the DTO object it showing me null. 在使用Facebook api时,我遇到了一个名为start_time的字段,该字段的类型为datetime,我在DTO类中声明了一个start_time字段作为字符串,而我将json转换为Java时,我可以获取start_time并在dto对象中进行填充但是,当我尝试从DTO对象检索时,显示为null。

DataDTO data  = new DataDTO();
String start_time = item.getString("start_time").toString();    
data.setStart_time(start_time);
System.out.println(start_time )   // 2016-03-12T22:00:00-0300
System.out.println(data.getStart_time()); // getting null

my DTO class 我的DTO课程

public class DataDTO implements Serializable{

    private String id;
    private String name;

    private String start_time;
    private String timezone;
    private String location;    
    private String end_time;

    //getters and setters
}

my sample json data 我的样本json数据

{  
      "id":"1443832172536969",
      "timezone":"America/New_York",
      "location":"Atlanta, GA, United States",
      "name":"Atlanta Dream Tour 2015",
      "start_time":"2016-03-12T22:00:00-0300"
   }

When i googled i came to know that DateTime is something belongs to joda time api what exactly it is? 当我用Google搜索时,我知道DateTime是属于joda time api的东西,它究竟是什么? Do i need to include any jar files in my project for joda-time api? 我是否需要在我的项目中为Joda-Time api包含任何jar文件?

I tried creating DateTime dt = new DateTime(); 我尝试创建DateTime dt = new DateTime(); in my method it showing DateTime cannot be resolved to a type. 在我的方法中,它显示DateTime无法解析为一种类型。

I am using java version 1.7.0_40 does this support joda-time. 我正在使用Java版本1.7.0_40来支持joda-time。 i tried converting to string using toString() method but no use. 我尝试使用toString()方法转换为字符串,但没有用。 Please help me how can i convert DateTime to string? 请帮我如何将DateTime转换为字符串?

Am i misunderstanding something? 我误会了吗?
The only reason this can happen is because data.setStart_time(start_time); 发生这种情况的唯一原因是因为data.setStart_time(start_time); is not working or the data.getStart_time() is not implemented properly. 无法正常工作或data.getStart_time()未正确实现。
There is no conversion required between DateTime and String. DateTime和String之间不需要转换。

如果您只想将开始日期更改为字符串,则可以使用

start_time_string = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",java.util.Locale.getDefault()).parse(start_time);

Are you using Maven? 您在使用Maven吗? Have you added the joda-time dependency on your pom.xml? 您是否已在pom.xml中添加了joda-time依赖项? In case you don't you should add something like this: 如果您不这样做,则应添加以下内容:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.8</version>
</dependency>

More info can be found here . 更多信息可以在这里找到。 Hope it helps! 希望能帮助到你!

Try Calendar : 尝试日历:

Calendar c = Calendar.getInstance();
        System.out.println("Seconds : "+c.get(Calendar.SECOND));
        System.out.println("Minute : "+c.get(Calendar.MINUTE));
        System.out.println("Hour of day : "+c.get(Calendar.HOUR_OF_DAY));
        System.out.println("Date : "+c.get(Calendar.DATE));
        System.out.println("Day : "+c.get(Calendar.DAY_OF_WEEK));
        System.out.println("Month : "+c.get(Calendar.MONTH));
        System.out.println("Year : "+c.get(Calendar.YEAR)); 

And format it to your requirement. 并将其格式化为您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM