简体   繁体   中英

How to add Date and Time in RFC3339 format in java?

I have a date in string and a time in string ,now I just need to add them in RFC 3339 format and insert it into event object which is not taking string. u can see my code below

    String date_from=req.getParameter("date_from").toString();
    String date_end=req.getParameter("date_end").toString();
    String time_from=req.getParameter("time_from").toString();
    String time_to=req.getParameter("time_to").toString();

     // here i m converting date_from which is string into Date object
    java.sql.Date dateFrom=  java.sql.Date.valueOf(date_from);
    java.sql.Date dateTo=  java.sql.Date.valueOf(date_end);




    String dateTimeStart=date_from+"T"+time_from+"Z";
    String dateTimeTo=date_end+"T"+time_to+"Z";

       //now here i just need to convert (string)dateTimeStart into object so that i able to use into my event class

           Event event = new Event();
           event.setSummary(userName);

           DateTime start = new DateTime(dateFrom, TimeZone.getTimeZone("Asia/Kolkata"));
           DateTime end = new DateTime(dateTo, TimeZone.getTimeZone("Asia/Kolkata"));

Hi , i am trying to make more clearer what my doubt is :

   DateTime end = new DateTime(dateTo, TimeZone.getTimeZone("Asia/Kolkata"));

see the above line of code , in first argument i want the format as "YYYY-MM-ddTHH:mm:ssZ" the date "yyyy-MM-dd" i have in variable "dateTo" it is not containing the time with it.so to get time in hour , inutes , and seconds i am taking time in the variable time_to

      String time_to=req.getParameter("time_to").toString();

I used the following piece of code to convert into date object and used above in first line

     java.sql.Date dateTo=  java.sql.Date.valueOf(date_end);

same i did with time , but when i try to concate both date and time it give me a error when i use it as first argument in the following line of code

    DateTime end = new DateTime(dateTo, TimeZone.getTimeZone("Asia/Kolkata"));

Use new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX") to parse/format the dates. Check SimpleDateFormat JavaDoc for details.

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