Java Parse Time parseTime(String text)

Here you can find the source of parseTime(String text)

Description

parse Time

License

Apache License

Declaration

public static Date parseTime(String text) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern TZ_REGEX = Pattern.compile("([+-][0-9][0-9]):?([0-9][0-9])$");

    public static Date parseTime(String text) {
        if (text == null)
            return null;
        Matcher matcher = TZ_REGEX.matcher(text);
        TimeZone timeZone = null;
        if (matcher.find()) {
            String tzCode = "GMT" + matcher.group(1) + matcher.group(2); // eg "GMT+0100"
            timeZone = TimeZone.getTimeZone(tzCode);
        }//from ww  w .  j a v  a2  s . c om
        DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        if (timeZone != null) {
            formatter.setTimeZone(timeZone);
        }
        try {
            Date result = formatter.parse(text);
            return result;
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. parseTime(String s)
  2. parseTime(String sTime)
  3. parseTime(String str)
  4. parseTime(String str)
  5. parseTime(String t)
  6. parseTime(String time)
  7. parseTime(String time)
  8. parseTime(String time, String formatStrBefore, String formatStrAfter)
  9. parseTime(String timestring)