Java Time Format formatTime(long seconds)

Here you can find the source of formatTime(long seconds)

Description

format Time

License

Apache License

Declaration

public static String formatTime(long seconds) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String formatTime(long seconds) {
        String timeText = "";
        seconds += 1L;//  w  w w .j  av a  2  s. com
        if (seconds % (60 * 60 * 24 * 365) >= 0) {
            int timecalc = (int) Math.floor(seconds / (60 * 60 * 24 * 365));
            seconds = seconds % (60 * 60 * 24 * 365);
            if (timecalc != 0) {
                if (timecalc == 1) {
                    timeText += timecalc + " year,";
                } else {
                    timeText += timecalc + " years,";
                }
            }
        }

        if (seconds % (30 * 24 * 60 * 60) >= 0) {
            int timecalc = (int) Math.floor(seconds / (30 * 24 * 60 * 60));
            seconds = seconds % (30 * 24 * 60 * 60);
            if (timecalc != 0) {
                if (timecalc == 1) {
                    timeText += " " + timecalc + " month,";
                } else {
                    timeText += " " + timecalc + " months,";
                }
            }
        }

        if (seconds % (60 * 60 * 24 * 7) >= 0) {
            int timecalc = (int) Math.floor(seconds / (60 * 60 * 24 * 7));
            seconds = seconds % (60 * 60 * 24 * 7);
            if (timecalc != 0) {
                if (timecalc == 1) {
                    timeText += " " + timecalc + " week,";
                } else {
                    timeText += " " + timecalc + " weeks,";
                }
            }
        }

        if (seconds % (60 * 60 * 24) >= 0) {
            int timecalc = (int) Math.floor(seconds / (60 * 60 * 24));
            seconds = seconds % (60 * 60 * 24);
            if (timecalc != 0) {
                if (timecalc == 1) {
                    timeText += " " + timecalc + " day,";
                } else {
                    timeText += " " + timecalc + " days,";
                }
            }
        }

        if (seconds % (60 * 60) >= 0) {
            int timecalc = (int) Math.floor(seconds / (60 * 60));
            seconds = seconds % (60 * 60);
            if (timecalc != 0) {
                if (timecalc == 1) {
                    timeText += " " + timecalc + " hour,";
                } else {
                    timeText += " " + timecalc + " hours,";
                }
            }
        }

        if (seconds % 60 >= 0) {
            int timecalc = (int) Math.floor(seconds / (60));
            seconds = seconds % (60);
            if (timecalc != 0) {
                if (timecalc == 1) {
                    timeText += " " + timecalc + " minute,";
                } else {
                    timeText += " " + timecalc + " minutes,";
                }
            }
        }

        if (seconds > 0) {
            if (seconds == 1) {
                timeText += " " + seconds + " second,";
            } else {
                timeText += " " + seconds + " seconds,";
            }
        }
        if (timeText.length() > 0) {
            timeText = timeText.substring(0, timeText.length() - 1);
            int lastComma = timeText.lastIndexOf(",");
            if (lastComma != -1) {
                timeText = timeText.substring(1, lastComma) + " and "
                        + timeText.substring(lastComma + 2, timeText.length());
            } else {
                timeText = timeText.substring(1);
            }
        } else {
            timeText = "0 seconds";
        }
        return timeText;
    }
}

Related

  1. formatTime(long msElapsed)
  2. formatTime(Long num)
  3. formatTime(long sec)
  4. formatTime(long seconds)
  5. formatTime(long seconds)
  6. formatTime(long time)
  7. formatTime(long time)
  8. formatTime(long time)
  9. formatTime(long time)