view software/mpdl-services/mpiwg-mpdl-xml/src/de/mpg/mpiwg/berlin/mpdl/util/Util.java @ 23:e845310098ba

diverse Korrekturen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Nov 2012 12:35:19 +0100
parents dc5e9fcb3fdc
children
line wrap: on
line source

package de.mpg.mpiwg.berlin.mpdl.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;

public class Util {

  public Properties getProperties(String fullFileName) {
    Properties props = new Properties(); 
    try {
      File file = new File(fullFileName);
      FileInputStream in = new FileInputStream(file);
      props.load(in);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return props;
  }

  public String toYearStr(String inputStr) {
    String retYearStr = inputStr.trim();
    int index = inputStr.indexOf("-");
    if (index > 0) {
      retYearStr = inputStr.substring(0, index);
      retYearStr = retYearStr.trim();
    }
    return retYearStr;
  }

  public Date toDate(String xsDateStr) throws ApplicationException {
    Date retDate = null;
    if (xsDateStr == null)
      return null;
    try {
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
      retDate = dateFormat.parse(xsDateStr);
    } catch (ParseException e) {
      throw new ApplicationException(e);
    }
    return retDate;
  }

  public String toXsDate(Date date) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    String xsDateStr = dateFormat.format(date);
    return xsDateStr;
  }
  
  public Double getSecondWithMillisecondsBetween(Date begin, Date end) {
    long beginMS = begin.getTime();
    long endMS = end.getTime();
    long elapsedSeconds = (endMS - beginMS) / 1000;
    long elapsedMilliSecondsAfterSeconds1 = (endMS - beginMS) - (elapsedSeconds * 1000);
    Double seconds = new Double(elapsedSeconds + "." + elapsedMilliSecondsAfterSeconds1); 
    return seconds;
  }
 
}