简体   繁体   中英

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.

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

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

{  
      "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? Do i need to include any jar files in my project for joda-time api?

I tried creating DateTime dt = new DateTime(); in my method it showing DateTime cannot be resolved to a type.

I am using java version 1.7.0_40 does this support joda-time. i tried converting to string using toString() method but no use. Please help me how can i convert DateTime to string?

Am i misunderstanding something?
The only reason this can happen is because data.setStart_time(start_time); is not working or the data.getStart_time() is not implemented properly.
There is no conversion required between DateTime and 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? Have you added the joda-time dependency on your pom.xml? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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