Java Timestamp Parse parseTimestamp(String time)

Here you can find the source of parseTimestamp(String time)

Description

Parse the time

License

Open Source License

Parameter

Parameter Description
time should be in the format of xs:dateTime. e.g. <code> 2016-04-05T12:00:00 </code>

Return

true if the time stamp of the index file has changed

Declaration

private static boolean parseTimestamp(String time) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Liviu Ionescu.//from   ww  w  . ja  v a 2 s  .com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Liviu Ionescu - initial implementation.
 *     ARM Ltd and ARM Germany GmbH - application-specific implementation
 *******************************************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static long timestamp = 0;

    /**
     * Parse the time
     * @param time should be in the format of xs:dateTime.
     * e.g. <code> 2016-04-05T12:00:00 </code>
     * @return true if the time stamp of the index file has changed
     */
    private static boolean parseTimestamp(String time) {
        StringBuilder dateFormat = new StringBuilder("yyyy-MM-dd'T'HH:mm:ss"); //$NON-NLS-1$
        // if the date contains time zone info, add a 'Z' in the end of the date format
        int fromIndex = time.lastIndexOf(':');
        if (time.indexOf('+', fromIndex) > 0 || time.indexOf('-', fromIndex) > 0) {
            dateFormat.append('Z');
        }
        try {
            Date date = new SimpleDateFormat(dateFormat.toString()).parse(time);
            long ms = date.getTime();
            if (ms != timestamp) {
                timestamp = ms;
                return true;
            }
        } catch (ParseException e) {
        }
        return false;
    }
}

Related

  1. parseTimestamp(String s)
  2. parseTimestamp(String s)
  3. parseTimestamp(String sDate, String sFormat)
  4. parseTimestamp(String src, String pattern)
  5. parseTimestamp(String strDate)
  6. parseTimestamp(String timestamp)
  7. parseTimeStamp(String timeStamp)
  8. parseTimestamp(String timestamp)
  9. parseTimestamp(String timestamp)