A few seconds ago, a few minutes ago, a few hours ago, a few days ago, a few months ago, a few years ago, java Implementation

Source: Internet
Author: User

A few seconds ago, a few minutes ago, a few hours ago, a few days ago, a few months ago, a few years ago, java Implementation

Package com. zuidaima; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; public class RelativeDateFormat {private static final long ONE_MINUTE = 60000L; private static final long ONE_HOUR = 3600000L; private static final long ONE_DAY = 86400000L; private static final long ONE_WEEK = 6048000l; private static final String ONE_SECOND_AGO = "seconds ago"; private static fi Nal String ONE_MINUTE_AGO = "Minutes Ago"; private static final String ONE_HOUR_AGO = "Hours Ago"; private static final String ONE_DAY_AGO = "Days Ago "; private static final String ONE_MONTH_AGO = "Months Ago"; private static final String ONE_YEAR_AGO = "years ago"; public static void main (String [] args) throws ParseException {SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: m: s"); Date date = format. parse ("2013-11-11 18:35:35 "); System. out. println (format (date);} public static String format (Date date) {long delta = new Date (). getTime ()-date. getTime (); if (delta <1L * ONE_MINUTE) {long seconds = toSeconds (delta); return (seconds <= 0? 1: seconds) + ONE_SECOND_AGO;} if (delta <45L * ONE_MINUTE) {long minutes = toMinutes (delta); return (minutes <= 0? 1: minutes) + ONE_MINUTE_AGO;} if (delta <24L * ONE_HOUR) {long hours = toHours (delta); return (hours <= 0? 1: hours) + ONE_HOUR_AGO;} if (delta <48L * ONE_HOUR) {return "yesterday";} if (delta <30L * ONE_DAY) {long days = toDays (delta ); return (days <= 0? 1: days) + ONE_DAY_AGO;} if (delta <12L * 4L * ONE_WEEK) {long months = toMonths (delta); return (months <= 0? 1: months) + ONE_MONTH_AGO;} else {long years = toYears (delta); return (years <= 0? 1: years) + ONE_YEAR_AGO;} private static long toSeconds (long date) {return date/1000L;} private static long toMinutes (long date) {return toSeconds (date) /60L;} private static long toHours (long date) {return toMinutes (date)/60L;} private static long toDays (long date) {return toHours (date)/24L ;} private static long toMonths (long date) {return toDays (date)/30L;} private static long toYears (long date) {return toMonths (date)/365L ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.