Get last quarter of current time in android?

142 Views Asked by At

I am tying to get last quarter of current time in android.

for example the time is 12:45:00 and I need to have:

12:30:00

12:45:00

or the time is 10:50:00 and I need to have :

10:35:00

10:50:00

etc.

I write this code and I get result true :

    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    cal.setTime(cal.getTime());
    cal.add(Calendar.MINUTE, -15);
    String oneQuarterBack = dateFormat.format(cal.getTime());

But If I had 12:45:38 how I can calculate seconds ?

1

There are 1 best solutions below

0
On BEST ANSWER
Date date = new Date();
int lastQuarterInMillis = 900000;
date.setTime(System.currentTimeMillis() - lastQuarterInMillis);
SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss");
String lastQuarter = df.format(date);