Java SQL Date From convertIntoDuration(Long time)

Here you can find the source of convertIntoDuration(Long time)

Description

convert Into Duration

License

Open Source License

Declaration

public static String convertIntoDuration(Long time) 

Method Source Code


//package com.java2s;
/* //  w ww  . j  ava 2  s  .c o  m
 * Copyright (C) 2017 GetMangos
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import java.sql.Timestamp;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.time.LocalDate;

import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

public class Main {
    public static String convertIntoDuration(Long time) {
        int day = (int) TimeUnit.SECONDS.toDays(time);
        long hours = TimeUnit.SECONDS.toHours(time) - (day * 24);
        long minute = TimeUnit.SECONDS.toMinutes(time) - (TimeUnit.SECONDS.toHours(time) * 60);
        long second = TimeUnit.SECONDS.toSeconds(time) - (TimeUnit.SECONDS.toMinutes(time) * 60);

        StringBuilder sb = new StringBuilder();

        sb.append(day).append("d ").append(hours).append("h ").append(minute).append("m ").append(second)
                .append("s");

        return sb.toString();
    }

    public static String toString(Timestamp timestamp) {
        DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault());
        return format.format(new Date(timestamp.getTime()));
    }

    public static String toString(LocalDate date) {
        DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault());
        return format.format(date);
    }

    public static String toString(Date date) {
        DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.getDefault());
        return format.format(date);
    }
}

Related

  1. calendarToSqlDate(Calendar c)
  2. calendarToSqlDate(final Calendar dt)
  3. convertDateTimeString(String dstr)
  4. convertDateToTime(Date date, boolean am)
  5. convertDateToTime(final Date date)
  6. convertJavaTimeToJavaSql(Object x)
  7. convertNanoToTime(long nanos)
  8. convertTime(long time)
  9. convertTime(String time)