Here you can find the source of getTimeFromEpochMilli(String value, boolean shortFormat, String errorText)
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 |
public static String getTimeFromEpochMilli(String value, boolean shortFormat, String errorText)
//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; } } }