Changeset 30860


Ignore:
Timestamp:
10/13/16 13:08:33 (4 years ago)
Author:
kjdon
Message:

added in formatTimestamp method to convert a timestamp to human readable date, time, datetime, daysago.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XSLTUtil.java

    r30830 r30860  
    2323import java.io.UnsupportedEncodingException;
    2424import java.net.URLEncoder;
     25import java.text.DateFormat;
    2526import java.text.SimpleDateFormat;
    2627import java.util.ArrayList;
     
    513514    }
    514515
     516  public static final int TS_SECS = 0;
     517  public static final int TS_MILLISECS = 1;
     518  public static final int F_DATE = 0;
     519  public static final int F_TIME = 1;
     520  public static final int F_DATETIME = 2;
     521  public static final int F_DAYSAGO = 3;
     522
     523  public static String formatTimeStamp(String timestamp, int ts_type, int format_type, String lang) {
     524    try {
     525      long ts = Long.parseLong(timestamp);
     526      if (ts_type == TS_SECS) {
     527    ts = ts * 1000;
     528      }
     529      if (format_type == F_DAYSAGO) {
     530    long current_time = new Date().getTime();
     531    long days = (current_time - ts)/86400000;
     532    return String.valueOf(days);
     533      }
     534      Date d = new Date(ts);
     535      DateFormat df;
     536      switch (format_type) {
     537      case F_DATE:
     538    df = DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale(lang));
     539    break;
     540      case F_TIME:
     541    df = DateFormat.getTimeInstance(DateFormat.DEFAULT, new Locale(lang));
     542    break;
     543      case F_DATETIME:
     544    df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, new Locale(lang));
     545    break;
     546      default:
     547    df = DateFormat.getDateInstance(DateFormat.DEFAULT, new Locale(lang));
     548    break;
     549      }
     550     
     551      return df.format(d);
     552    } catch (Exception e) {
     553     
     554      return timestamp + e.getMessage();
     555    }
     556   
     557  }
    515558    public static String getDetailFromDate(String date, String detail, String lang)
    516559    {
Note: See TracChangeset for help on using the changeset viewer.