Java Date Format As formatDateAccordingToSessionDuration(Calendar cal, int sessionDuration, int offset)

Here you can find the source of formatDateAccordingToSessionDuration(Calendar cal, int sessionDuration, int offset)

Description

format Date According To Session Duration

License

Open Source License

Declaration

public static String formatDateAccordingToSessionDuration(Calendar cal, int sessionDuration, int offset) 

Method Source Code

//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);
    }
}

Related

  1. fmtDate(Date date)
  2. fmtDateTime(final Date dateTime, final String simpleDateTimeFormat)
  3. fmtMsg(final String fmt, final String arg)
  4. fmtNumStringToDouble(String s)
  5. fmtTime(Date time)
  6. formatDateAppello(Date data)
  7. formatDateAsFileSystemName()
  8. formatDateAsYYYYMMDD(Date date)
  9. formatDateBdChar(Date birthdate)