0

I have the following code to get datetime UTC.

public static Date GetUTCdatetimeAsDate()
{
    return StringDateToDate(GetUTCdatetimeAsString());
}

public static String GetUTCdatetimeAsString()
{
    final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    final String utcTime = sdf.format(new Date());

    return utcTime;
}

public static Date StringDateToDate(String StrDate)
{
    Date dateToReturn = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);

    try
    {
        dateToReturn = (Date)dateFormat.parse(StrDate);
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }

    return dateToReturn;
}

Now I want to use GETUTCdatetimeAsDate and if I get for example 11/22/2014 03:12 the minutes will be rounded up to the nearest 5 minutes. So in this case that will be 11/22/2014 03:15. If it is 11/22/2014 03:31 it will be 11/22/2014 03:35.

Any ideas on how to achieve this?

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94

1 Answers1

3
public static Date StringDateToDate(String StrDate) {
    Date dateToReturn = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
    try {
        dateToReturn = (Date) dateFormat.parse(StrDate);
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }

    Calendar c = Calendar.getInstance();
    c.setTime(dateToReturn);
    int minute = c.get(Calendar.MINUTE);
    minute = minute % 5;
    if (minute != 0) {
        int minuteToAdd = 5 - minute;
        c.add(Calendar.MINUTE, minuteToAdd);
    }

    return c.getTime();
}
todd.tout
  • 111
  • 4