Get date of the last 7 days - Android java.util

Android examples for java.util:Day

Description

Get date of the last 7 days

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static SimpleDateFormat dateFormat2 = new SimpleDateFormat(
            "yyyy-MM-dd");
    public static SimpleDateFormat dateFormat3 = new SimpleDateFormat(
            "MM/dd");

    /**//from   w  w w  . j  a va  2 s.  co m
     * Get date of the last 7 days
     * @return day of last 7 days in "yyyy-MM-dd" and "MM/dd" format
     */
    public static String[] weekDay() {
        String[] weekDay = new String[14];
        Date date = new Date();
        long oneDay = 1000 * 3600 * 24;
        for (int i = 0; i < 7; i++) {
            Date tmp = new Date();
            tmp.setTime(date.getTime() - oneDay * i);
            weekDay[6 - i] = dateFormat2.format(tmp);
            weekDay[13 - i] = dateFormat3.format(tmp);
        }
        return weekDay;
    }
}

Related Tutorials