Java DateTimeFormatter getTimeFromEpochMilli(String value, boolean shortFormat, String errorText)

Here you can find the source of getTimeFromEpochMilli(String value, boolean shortFormat, String errorText)

Description

Converts a number of milliseconds since 1970 to an regular date

License

Open Source License

Parameter

Parameter Description
value The number of milliseconds as string
shortFormat Controls the output format: "dd. MMMM yyyy, HH:mm" (false) or "dd.MM.yy HH:mm" (true)
errorText The string the should be returned if the conversion fails

Return

The date as string with the selected format or the text specified by "errorText"

Declaration

public static String getTimeFromEpochMilli(String value, boolean shortFormat, String errorText) 

Method Source Code

//package com.java2s;
/* KeyMinder/*w  w  w  . j  a  v  a 2s. co  m*/
 * Copyright (C) 2015-2016 Bastian Kraemer
 *
 * Utilities.java
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

import java.time.DateTimeException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class Main {
    /**
     * Converts a number of milliseconds since 1970 to an regular date
     * @param value The number of milliseconds as string
     * @param shortFormat Controls the output format: "dd. MMMM yyyy, HH:mm" (false) or "dd.MM.yy HH:mm" (true)
     * @param errorText The string the should be returned if the conversion fails
     * @return The date as string with the selected format or the text specified by "errorText"
     */
    public static String getTimeFromEpochMilli(String value, boolean shortFormat, String errorText) {
        try {
            return Instant.ofEpochMilli(Long.parseLong(value)).atZone(ZoneId.systemDefault())
                    .format(DateTimeFormatter.ofPattern(shortFormat ? "dd.MM.yy HH:mm" : "dd. MMMM yyyy, HH:mm"));
        } catch (NumberFormatException | DateTimeException ex) {
            return errorText;
        }
    }
}

Related

  1. getShortDateManualEntryFormatter()
  2. getShortFormatter(Locale locale)
  3. getStartOfDay(Date dateToFormat)
  4. getTime(final String time, final DateTimeFormatter formatter)
  5. getTime(String format)
  6. getTimeStamp(final String dateTimeFormatPattern)
  7. getTimestampedPath(Path baseFile, TemporalAccessor time, DateTimeFormatter formatter)
  8. parse(DateTimeFormatter formatter, String string)
  9. parse(final DateTimeFormatter formatter, final String value, final ZoneId zoneId)