Sleuth Kit Java Bindings (JNI)  4.11.1
Java bindings for using The Sleuth Kit
TimeUtilities.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 package org.sleuthkit.datamodel;
21 
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.TimeZone;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 
32 public class TimeUtilities {
33  private static final Logger LOGGER = Logger.getLogger(TimeUtilities.class.getName());
34  private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
35 
36  private TimeUtilities(){
37  }
38 
46  public static String epochToTime(long epoch) {
47  String time = "0000-00-00 00:00:00";
48  if (epoch != 0) {
49  time = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(new java.util.Date(epoch * 1000));
50  }
51  return time;
52  }
53 
63  public static String epochToTime(long epoch, TimeZone tzone) {
64  String time = "0000-00-00 00:00:00";
65  if (epoch != 0) {
66  synchronized (DATE_FORMATTER) {
67  DATE_FORMATTER.setTimeZone(tzone);
68  time = DATE_FORMATTER.format(new java.util.Date(epoch * 1000));
69  }
70  }
71  return time;
72  }
73 
82  public static String epochToTimeISO8601(long epoch, TimeZone tzone) {
83  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
84  formatter.setTimeZone(tzone);
85  return formatter.format(new Date(epoch));
86  }
87 
95  public static long timeToEpoch(String time) {
96  long epoch = 0;
97  try {
98  epoch = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime() / 1000;
99  } catch (Exception e) {
100  LOGGER.log(Level.WARNING, "Failed to parse time string", e); //NON-NLS
101  }
102 
103  return epoch;
104  }
105 }
static long timeToEpoch(String time)
static String epochToTime(long epoch)
static String epochToTimeISO8601(long epoch, TimeZone tzone)
static String epochToTime(long epoch, TimeZone tzone)

Copyright © 2011-2021 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.