如何解析从String到Date对象的时间?

时间:2014-07-27 06:41:47

标签: java

我有一个解析String time to date对象的代码。但是,当String中的时间格式为HH:mm:ss例如。 23:34:45,日期对象在解析后将其更改为某个不同的时间。我使用过timezone,但找不到保留原始时间的方法。我怎样才能做到这一点? 这些属性给出了错误的值: timeLine,timeSystem,timeGapDate

            timeFromLine = getTimeFromLine(line);
            timeFromSystem= getSystemTime();

            timeGap= propertiesReader.getPropertyValue("timeGap");
            sf1 = new SimpleDateFormat("HH:mm:ss");
            sf1.setTimeZone(TimeZone.getTimeZone("GMT"));

            //sf1.setLenient(false);
            timeLine= sf1.parse(timeFromLine);
            timeSystem=sf1.parse(timeFromSystem);
            timeGapDate=sf1.parse(timeGap);
            startTimeLong=timeSystem.getTime();
            timeGapLong=timeGapDate.getTime();
            endTimeLong=startTimeLong-timeGapLong;
            Date d= new Date(endTimeLong);
            endTimeString= sf1.format(d);
            endTimeDate1=sf1.parse(endTimeString);

Method- getSystemTime();

private static String getSystemTime() {
SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm:ss");//dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(now);
return strDate;
}

方法getTimeFromLine();

private static String getTimeFromLine(String s){
final Scanner sc = new Scanner(s);
String date2 ="";

while(sc.hasNextLine()) {
    final String line2 = sc.nextLine();
    String[] number = line2.split(Constants.NUMBER_REGEX);
    String firstSplitValue3= number[3];
    date2=firstSplitValue3;

}
sc.close();
return date2;

}
}

时间差距只是00:06:00 我得到的输出就是这个,

timeFromLine- 23:59:15 timeFromSystem- 12:41:51 timeLine- Fri Jan 02 05:29:15 IST 1970 timeSystem- Thu Jan 01 18:11:51 IST 1970 timeGapDate- Thu Jan 01 05:36:00 IST 1970

但我想要输出 timeLine- 23:59:15 timeSystem- 12:41:51 timeGapDate- 00:06:00

1 个答案:

答案 0 :(得分:1)

这对我有用:

SimpleDateFormat sf1 = new SimpleDateFormat("HH:mm:ss");
String timeFromSystem = getSystemTime();    // your method, unchanged
Date timeSystem = sf1.parse(timeFromSystem);
System.out.println( timeFromSystem + " => " + timeSystem );

结果:

09:19:38 => Thu Jan 01 09:19:38 CET 1970

我不相信getTimeFromLine(String s)是正确的,不是没有看到正则表达式,而是解释为什么需要循环来输入单行。

<强>后来

public static long gapInMillis( String gap ){
    String[] hms = gap.split( ":" );
    return ((Integer.valueOf( hms[0] ) * 60 +
             Integer.valueOf( hms[1] )) * 60 +
             Integer.valueOf( hms[2] )) * 1000;
}

public static void main(String args[] ) throws Exception {
    Date now = new Date();
    Date nowMinusGap = new Date( now.getTime() - gapInMillis( gap ) );
    System.out.println( nowMinusGap + " - " + now );
}

这很简单直接。 - 不确定标准输入的行应该做什么,代码太混淆了。

稍后

String line = "Jul 06 23:59:15";  // add currrent year and modify format
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd HH:mm:ss");
Date timeFromLine = sdf.parse( line );
System.out.println( "line: " + timeFromLine );

这将从您输入行的某个部分解析日期,但结果日期将是1970年。您可以添加当前年份,但需要考虑12月31日/ 1月1日。

这很危险:

String line = (now.getYear()+1900) + " " + "Jul 27 12:50:15";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");

if( timeFromLine.after( nowMinusGap ) &&
    timeFromLine.before( now ) ){
    System.out.println( "line: " + timeFromLine + " in range" );
}

处理12月31日/ 1月1日

一些测试数据和getLine:

static String[] lines = new String[]{
    "Dec 31 19:50:15 ---",
    "Dec 31 21:50:15 ---",
    "Dec 31 23:50:15 ---",
    "Jan 1 00:50:15 ---",
    "Jan 1 01:50:15 ---" };
static int pos = 0;
static String getLine(){
return pos < lines.length ? lines[pos++] : null;
}

过滤线条的过程:注意现在的虚假设置

static SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
static void selectLines() throws ParseException {
    Date now = new Date();
    now = sdf.parse( "2014 Jan 1 02:00:00" ); // fake, just for testing
    Date nowMinusGap = new Date( now.getTime() - gapInMillis( gap ) );
    System.out.println( nowMinusGap + " - " + now );

    String line;
    while( (line = getLine()) != null ){
        Date timeFromLine = null;
        for( int year = now.getYear()+1900; year > 2000; year-- ){
            String lineToParse = year + " " + line;
            timeFromLine = sdf.parse( lineToParse );
            if( timeFromLine.before( now ) ) break;
        }
        System.out.println( "line: " + timeFromLine );
        if( timeFromLine.after( nowMinusGap ) &&
            timeFromLine.before( now ) ){
            System.out.println( "line: " + timeFromLine + " in range" );
        }
    }
}