Android get date before 7 days (one week)

Viewed 75812

How to get date before one week from now in android in this format:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

ex: now 2010-09-19 HH:mm:ss, before one week 2010-09-12 HH:mm:ss

Thanks

8 Answers

Try this

One single method for getting the date from current or bypassing any date

@Pratik Butani's second method for getting the date from our own date is not working at my end.

Kotlin

fun getCalculatedDate(date: String, dateFormat: String, days: Int): String {
    val cal = Calendar.getInstance()
    val s = SimpleDateFormat(dateFormat)
    if (date.isNotEmpty()) {
        cal.time = s.parse(date)
    }
    cal.add(Calendar.DAY_OF_YEAR, days)
    return s.format(Date(cal.timeInMillis))
}

Java

 public static String getCalculatedDate(String date,String dateFormat, int days) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat s = new SimpleDateFormat(dateFormat);
    if (!date.isEmpty()) {
        try {
            cal.setTime(s.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    cal.add(Calendar.DAY_OF_YEAR, days);
    return s.format(new Date(cal.getTimeInMillis()));
}

Usage

  1. getCalculatedDate("", "yyyy-MM-dd", -2) // If you want date from today
  2. getCalculatedDate("2019-11-05", "yyyy-MM-dd", -2) // If you want date from your own
public static Date getDateWithOffset(int offset, Date date){
    Calendar calendar = calendar = Calendar.getInstance();;
    calendar.setTime(date);
    calendar.add(Calendar.DAY_OF_MONTH, offset);
    return calendar.getTime();
}

Date weekAgoDate = getDateWithOffset(-7, new Date());

OR using Joda:

add Joda library

    implementation 'joda-time:joda-time:2.10'

'

DateTime now = new DateTime();
DateTime weekAgo = now.minusWeeks(1);
Date weekAgoDate = weekAgo.toDate()// if you want to convert it to Date

-----------------------------UPDATE-------------------------------

Use Java 8 APIs or ThreeTenABP for Android (minSdk<24).

ThreeTenABP:

implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1'

'

LocalDate now= LocalDate.now();
now.minusWeeks(1);

You can use this code for get exact string which you want.

object DateUtil{
    fun timeAgo(context: Context, time_ago: Long): String {
        val curTime = Calendar.getInstance().timeInMillis / 1000
        val timeElapsed = curTime - (time_ago / 1000)
        val minutes = (timeElapsed / 60).toFloat().roundToInt()
        val hours = (timeElapsed / 3600).toFloat().roundToInt()
        val days = (timeElapsed / 86400).toFloat().roundToInt()
        val weeks = (timeElapsed / 604800).toFloat().roundToInt()
        val months = (timeElapsed / 2600640).toFloat().roundToInt()
        val years = (timeElapsed / 31207680).toFloat().roundToInt()

        // Seconds
        return when {
            timeElapsed <= 60 -> context.getString(R.string.just_now)
            minutes <= 60 -> when (minutes) {
                1 -> context.getString(R.string.x_minute_ago, minutes)
                else -> context.getString(R.string.x_minute_ago, minutes)
            }
            hours <= 24 -> when (hours) {
                1 -> context.getString(R.string.x_hour_ago, hours)
                else -> context.getString(R.string.x_hours_ago, hours)
            }
            days <= 7 -> when (days) {
                1 -> context.getString(R.string.yesterday)
                else -> context.getString(R.string.x_days_ago, days)
            }
            weeks <= 4.3 -> when (weeks) {
                1 -> context.getString(R.string.x_week_ago, weeks)
                else -> context.getString(R.string.x_weeks_ago, weeks)
            }
            months <= 12 -> when (months) {
                1 -> context.getString(R.string.x_month_ago, months)
                else -> context.getString(R.string.x_months_ago, months)
            }
            else -> when (years) {
                1 -> context.getString(R.string.x_year_ago, years)
                else -> context.getString(R.string.x_years_ago, years)
            }
        }
    }

}

Kotlin:

import java.util.*

val Int.week: Period
    get() = Period(period = Calendar.WEEK_OF_MONTH, value = this)

internal val calendar: Calendar by lazy {
    Calendar.getInstance()
}

operator fun Date.minus(duration: Period): Date {
    calendar.time = this
    calendar.add(duration.period, -duration.value)
    return calendar.time
}

data class Period(val period: Int, val value: Int)

Usage:

val newDate = oldDate - 1.week
// Or val newDate = oldDate.minus(1.week)
Related