formatted Date in long AS year or month or day - Android java.util

Android examples for java.util:Month

Description

formatted Date in long AS year or month or day

Demo Code


//package com.java2s;

public class Main {
    public static String formattedDate(Long timestamp) {
        Long currentTimestamp = System.currentTimeMillis() / 1000L;
        Long seconds = currentTimestamp - (timestamp / 1000L);
        Long minutes = seconds / 60;
        Long hours = minutes / 60;
        Long days = hours / 24;//w w w  .j  a v  a2  s . c  o m
        Long weeks = days / 7;
        Long months = weeks / 4;
        Long years = months / 12;

        if (years > 0) {
            return years + "y";
        } else if (months > 0) {
            return months + "m";
        } else if (weeks > 0) {
            return weeks + "w";
        } else if (days > 0) {
            return days + "d";
        } else if (hours > 0) {
            return hours + "h";
        } else if (minutes > 0) {
            return minutes + "m";
        } else {
            return seconds + "s";
        }
    }
}

Related Tutorials