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

后端 未结 4 1963
忘了有多久
忘了有多久 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:07

    There are these methods available to get the individual parts of a date

    getDate()
    getMinutes()
    getHours()
    getSeconds()
    getMonth()
    getTime()
    getTimezoneOffset()
    getYear()
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-15 21:15

    Try using : int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

    Here,

    • Calendar.HOUR_OF_DAY gives you the 24-hour time.
    • Calendar.HOUR gives you the 12-hour time.
    0 讨论(0)
  • 2020-12-15 21:25

    I'm suggesting that you store times in the DB as "timeInMillis". In my experience it simplifies code and it allows you to compare times values to eachother.

    To store a time:

    Calendar calendar = Calendar.getInstance(); // current time
    long timeInMillis = calendar.getTimeInMillis();
    mDb.saveTime (timeInMillis); // adjust this to work with your DB
    

    To retrieve a time:

    long timeInMillis = mDb.getTime();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis (timeInMillis);
    int milliSeconds = calendar.get(MILLISECOND);
    //etc
    
    0 讨论(0)
提交回复
热议问题