How to get the day, year, hours, min Individually from date format “yyyy-MM-dd'T'HH:mm:ss.SSSZ”?

后端 未结 4 1968
忘了有多久
忘了有多久 2020-12-15 21:00

I am doing a programme that stores the present time and date in \"yyyy-MM-dd\'T\'HH:mm:ss.SSSZ\" this format. and I am storing it in database as a string. when

4条回答
  •  春和景丽
    2020-12-15 21:11

    Just use parse instead of format :

    String dateFromDB = "";
    SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Date yourDate = parser.parse(dateFromDB);
    

    And then you can can read any field you want using java.util.Date & Calendar API :

      Calendar calendar = Calendar.getInstance();
        calendar.setTime(yourDate);
        calendar.get(Calendar.DAY_OF_MONTH); //Day of the month :)
        calendar.get(Calendar.SECOND); //number of seconds
    //and so on
    

    I hope it fits your needs

提交回复
热议问题