Java Long Number to Time prettyDate(long time)

Here you can find the source of prettyDate(long time)

Description

pretty Date

License

Apache License

Declaration

public static String prettyDate(long time) 

Method Source Code

//package com.java2s;
/*/*  www . j  a v a  2s .c  o  m*/
 * Copyright 2014 Alex Bennett & Alexander Chauncey
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static String prettyDate(long time) {
        long diff = (System.currentTimeMillis() - time) / 1000;
        double day_diff = Math.floor(diff / 86400);

        if (day_diff == 0 && diff < 60)
            return "just now";
        if (diff < 120)
            return "1 minute ago";
        if (diff < 3600)
            return Math.floor(diff / 60) + " minutes ago";
        if (diff < 7200)
            return "1 hour ago";
        if (diff < 86400)
            return Math.floor(diff / 3600) + " hours ago";
        if (day_diff == 1)
            return "Yesterday";
        if (day_diff < 7)
            return day_diff + " days ago";
        if (day_diff < 31)
            return Math.ceil(day_diff / 7) + " weeks ago";

        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(time));
    }
}

Related

  1. getUploadPath(String fileName, long time)
  2. logDuration(Logger logger, Level level, long startTime, String message)
  3. long2Str(long time)
  4. longToPgnDate(long time)
  5. longToTimedDate(long time)
  6. printDuration(String message, long startTime)
  7. printExecutionTime(long startTime, long endTime, String className, String methodName)
  8. str2Long(String time)
  9. stringTime(long time)