How To Get The Day, Year, Hours, Min Individually From Date Format "yyyy-mm-dd't'hh:mm:ss.sssz"?
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 i am collecting the data
Solution 1:
Just use parse instead of format :
StringdateFromDB="";
SimpleDateFormatparser=newSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateyourDate= 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
Solution 2:
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:
Calendarcalendar= Calendar.getInstance(); // current timelongtimeInMillis= 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
Solution 3:
There are these methods available to get the individual parts of a date
getDate()
getMinutes()
getHours()
getSeconds()
getMonth()
getTime()
getTimezoneOffset()
getYear()
Solution 4:
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.
Post a Comment for "How To Get The Day, Year, Hours, Min Individually From Date Format "yyyy-mm-dd't'hh:mm:ss.sssz"?"