Example usage for java.time LocalDateTime ofEpochSecond

List of usage examples for java.time LocalDateTime ofEpochSecond

Introduction

In this page you can find the example usage for java.time LocalDateTime ofEpochSecond.

Prototype

public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) 

Source Link

Document

Obtains an instance of LocalDateTime using seconds from the epoch of 1970-01-01T00:00:00Z.

Usage

From source file:Main.java

public static void main(String[] args) {
    //Getting date from the base date i.e 01/01/1970
    LocalDateTime dateFromBase = LocalDateTime.ofEpochSecond(10000, 0, ZoneOffset.UTC);
    System.out.println("10000th second time from 01/01/1970= " + dateFromBase);
}

From source file:com.speedment.examples.social.JSONImage.java

public static List<JSONImage> parseFrom(String json) {

    final JSONObject container = (JSONObject) JSONValue.parse(json);
    final JSONArray array = (JSONArray) container.get("images");
    final List<JSONImage> images = new ArrayList<>();

    array.stream().forEach(o -> {//w ww .j  av  a 2s  . co  m
        final JSONObject obj = (JSONObject) o;
        final JSONImage img = new JSONImage();
        final long time = Long.parseLong(obj.get("uploaded").toString());

        final LocalDateTime ldt = LocalDateTime.ofEpochSecond(time / 1000L, (int) (time % 1000) * 1000,
                ZoneOffset.UTC);

        img.title = obj.get("title").toString();
        img.description = obj.get("description").toString();
        img.uploaded = ldt;
        img.uploader = JSONUser.parse((JSONObject) obj.get("uploader"));
        img.image = fromBase64(obj.get("img_data").toString());
        images.add(img);
    });

    Collections.sort(images, Comparator.reverseOrder());

    return images;
}

From source file:com.oembedler.moon.graphql.engine.type.GraphQLLocalDateTimeType.java

public GraphQLLocalDateTimeType(String name, String description, String dateFormat) {
    super(name, description, new Coercing() {
        private final TimeZone timeZone = TimeZone.getTimeZone("UTC");

        @Override//from w  w  w .  jav a  2  s . co m
        public Object serialize(Object input) {
            if (input instanceof String) {
                return parse((String) input);
            } else if (input instanceof LocalDateTime) {
                return format((LocalDateTime) input);
            } else if (input instanceof Long) {
                return LocalDateTime.ofEpochSecond((Long) input, 0, ZoneOffset.UTC);
            } else if (input instanceof Integer) {
                return LocalDateTime.ofEpochSecond((((Integer) input).longValue()), 0, ZoneOffset.UTC);
            } else {
                throw new GraphQLException("Wrong timestamp value");
            }
        }

        @Override
        public Object parseValue(Object input) {
            return serialize(input);
        }

        @Override
        public Object parseLiteral(Object input) {
            if (!(input instanceof StringValue))
                return null;
            return parse(((StringValue) input).getValue());
        }

        private String format(LocalDateTime input) {
            return getDateTimeFormatter().format(input);
        }

        private LocalDateTime parse(String input) {
            LocalDateTime date = null;
            try {
                date = LocalDateTime.parse(input, getDateTimeFormatter());
            } catch (Exception e) {
                throw new GraphQLException("Can not parse input date", e);
            }
            return date;
        }

        private DateTimeFormatter getDateTimeFormatter() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
            return formatter;
        }
    });
    Assert.notNull(dateFormat, "Date format must not be null");
}

From source file:org.apache.hadoop.hive.metastore.HiveProtoEventsCleanerTask.java

/**
 * Compute the expired date partition, using the underlying clock in UTC time.
 *///  w w w  .j ava  2  s .  c  om
private static void computeExpiredDatePtn(long ttl) {
    // Use UTC date to ensure reader date is same on all timezones.
    LocalDate expiredDate = LocalDateTime.ofEpochSecond((clock.getTime() - ttl) / 1000, 0, ZoneOffset.UTC)
            .toLocalDate();
    expiredDatePtn = "date=" + DateTimeFormatter.ISO_LOCAL_DATE.format(expiredDate);
}

From source file:rjc.jplanner.model.DateTime.java

/****************************************** toString *******************************************/
public String toString(String format) {
    // convert to string in specified format
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(m_milliseconds / 1000L,
            (int) (m_milliseconds % 1000 * 1000000), ZoneOffset.UTC);

    // to support half-of-year using Bs, quote any unquoted Bs in format
    StringBuilder newFormat = new StringBuilder();
    boolean inQuote = false;
    boolean inB = false;
    char here;/*from w w w  . ja va  2  s.c o m*/
    for (int i = 0; i < format.length(); i++) {
        here = format.charAt(i);

        // are we in quoted text?
        if (here == QUOTE)
            inQuote = !inQuote;

        // replace unquoted Bs with special code
        if (inB && here == CHARB) {
            newFormat.append(CODE);
            continue;
        }

        // come to end of unquoted Bs
        if (inB && here != CHARB) {
            newFormat.append(QUOTE);
            inB = false;
            inQuote = false;
        }

        // start of unquoted Bs, start quote with special code
        if (!inQuote && here == CHARB) {
            // avoid creating double quotes
            if (newFormat.length() > 0 && newFormat.charAt(newFormat.length() - 1) == QUOTE) {
                newFormat.deleteCharAt(newFormat.length() - 1);
                newFormat.append(CODE);
            } else
                newFormat.append("'" + CODE);
            inQuote = true;
            inB = true;
        } else {
            newFormat.append(here);
        }
    }

    // close quote if quote still open
    if (inQuote)
        newFormat.append(QUOTE);

    String str = ldt.format(DateTimeFormatter.ofPattern(newFormat.toString()));

    // no special code so can return string immediately
    if (!str.contains(CODE))
        return str;

    // determine half-of-year
    String yearHalf;
    if (month() < 7)
        yearHalf = "1";
    else
        yearHalf = "2";

    // four or more Bs is not allowed
    String Bs = StringUtils.repeat(CODE, 4);
    if (str.contains(Bs))
        throw new IllegalArgumentException("Too many pattern letters: B");

    // replace three Bs
    Bs = StringUtils.repeat(CODE, 3);
    if (yearHalf.equals("1"))
        str = str.replace(Bs, yearHalf + "st half");
    else
        str = str.replace(Bs, yearHalf + "nd half");

    // replace two Bs
    Bs = StringUtils.repeat(CODE, 2);
    str = str.replace(Bs, "H" + yearHalf);

    // replace one Bs
    Bs = StringUtils.repeat(CODE, 1);
    str = str.replace(Bs, yearHalf);

    return str;
}

From source file:org.talend.dataprep.transformation.actions.date.TimestampToDate.java

protected String getTimeStamp(String from, DateTimeFormatter dateTimeFormatter) {
    if (!NumericHelper.isBigDecimal(from)) {
        // empty value if the date cannot be parsed
        return StringUtils.EMPTY;
    }//w  ww.  ja  va 2  s  .c  o m
    LocalDateTime date = LocalDateTime.ofEpochSecond(Long.parseLong(from), 0, ZoneOffset.UTC);
    return dateTimeFormatter.format(date);
}

From source file:com.ethlo.geodata.util.ResourceUtil.java

private static LocalDateTime formatDate(long timestamp) {
    return LocalDateTime.ofEpochSecond(timestamp / 1_000, 0, ZoneOffset.UTC);
}

From source file:io.mandrel.metrics.impl.MongoMetricsRepository.java

@Override
public Timeserie serie(String name) {
    Set<Data> results = StreamSupport.stream(timeseries.find(Filters.eq("type", name))
            .sort(Sorts.ascending("timestamp_hour")).limit(3).map(doc -> {
                LocalDateTime hour = LocalDateTime
                        .ofEpochSecond(((Date) doc.get("timestamp_hour")).getTime() / 1000, 0, ZoneOffset.UTC);
                Map<String, Long> values = (Map<String, Long>) doc.get("values");

                List<Data> mapped = values.entrySet().stream()
                        .map(elt -> Data.of(hour.plusMinutes(Long.valueOf(elt.getKey())), elt.getValue()))
                        .collect(Collectors.toList());
                return mapped;
            }).spliterator(), true).flatMap(elts -> elts.stream())
            .collect(TreeSet::new, Set::add, (left, right) -> {
                left.addAll(right);/*from  w w w . j a  v  a 2  s .c om*/
            });

    Timeserie timeserie = new Timeserie();
    timeserie.addAll(results);
    return timeserie;
}