Here you can find the source of formatDateAccordingToSessionDuration(Calendar cal, int sessionDuration, int offset)
public static String formatDateAccordingToSessionDuration(Calendar cal, int sessionDuration, int offset)
//package com.java2s; //License from project: Open Source License import java.text.DateFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class Main { /**//www . j a v a 2 s. c o m * A date format. */ private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm"); public static String formatDateAccordingToSessionDuration(Calendar cal, int sessionDuration, int offset) { long time = cal.getTimeInMillis(); long minutes = (time / (1000 * 60)); // calculate how much we need to subtract to get into the last session int diff = (int) (minutes % sessionDuration); // take into account the offset diff = diff - offset; if (diff < 0) diff += sessionDuration; time = (minutes - diff) * 1000 * 60; return df.format(new Date(time)); } /** * Formats a double. * * @param d * A double. * @return The formatted <i>String</i>. */ public static String format(double d) { NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH); nf.setMinimumFractionDigits(6); nf.setMaximumFractionDigits(6); return nf.format(d); } }