Package org.eclipse.jgit.util
Class GitDateParser
- java.lang.Object
-
- org.eclipse.jgit.util.GitDateParser
-
public class GitDateParser extends java.lang.Object
Parses strings with time and date specifications intoDate
. When git needs to parse strings specified by the user this parser can be used. One example is the parsing of the config parameter gc.pruneexpire. The parser can handle only subset of what native gits approxidate parser understands.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
GitDateParser.ParseableSimpleDateFormat
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.ThreadLocal<java.util.Map<java.util.Locale,java.util.Map<GitDateParser.ParseableSimpleDateFormat,java.text.SimpleDateFormat>>>
formatCache
static java.util.Date
NEVER
The Date representing never.
-
Constructor Summary
Constructors Constructor Description GitDateParser()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static java.text.SimpleDateFormat
getDateFormat(GitDateParser.ParseableSimpleDateFormat f, java.util.Locale locale)
private static java.text.SimpleDateFormat
getNewSimpleDateFormat(GitDateParser.ParseableSimpleDateFormat f, java.util.Locale locale, java.util.Map<GitDateParser.ParseableSimpleDateFormat,java.text.SimpleDateFormat> map)
static java.util.Date
parse(java.lang.String dateStr, java.util.Calendar now)
Parses a string into aDate
using the default locale.static java.util.Date
parse(java.lang.String dateStr, java.util.Calendar now, java.util.Locale locale)
Parses a string into aDate
using the given locale.private static java.util.Date
parse_relative(java.lang.String dateStr, java.util.Calendar now)
private static java.util.Date
parse_simple(java.lang.String dateStr, GitDateParser.ParseableSimpleDateFormat f, java.util.Locale locale)
-
-
-
Field Detail
-
NEVER
public static final java.util.Date NEVER
The Date representing never. Though this is a concrete value, most callers are adviced to avoid depending on the actual value.
-
formatCache
private static java.lang.ThreadLocal<java.util.Map<java.util.Locale,java.util.Map<GitDateParser.ParseableSimpleDateFormat,java.text.SimpleDateFormat>>> formatCache
-
-
Method Detail
-
getDateFormat
private static java.text.SimpleDateFormat getDateFormat(GitDateParser.ParseableSimpleDateFormat f, java.util.Locale locale)
-
getNewSimpleDateFormat
private static java.text.SimpleDateFormat getNewSimpleDateFormat(GitDateParser.ParseableSimpleDateFormat f, java.util.Locale locale, java.util.Map<GitDateParser.ParseableSimpleDateFormat,java.text.SimpleDateFormat> map)
-
parse
public static java.util.Date parse(java.lang.String dateStr, java.util.Calendar now) throws java.text.ParseException
Parses a string into aDate
using the default locale. Since this parser also supports relative formats (e.g. "yesterday") the caller can specify the reference date. These types of strings can be parsed:- "never"
- "now"
- "yesterday"
- "(x) years|months|weeks|days|hours|minutes|seconds ago"
Multiple specs can be combined like in "2 weeks 3 days ago". Instead of ' ' one can use '.' to separate the words - "yyyy-MM-dd HH:mm:ss Z" (ISO)
- "EEE, dd MMM yyyy HH:mm:ss Z" (RFC)
- "yyyy-MM-dd"
- "yyyy.MM.dd"
- "MM/dd/yyyy",
- "dd.MM.yyyy"
- "EEE MMM dd HH:mm:ss yyyy Z" (DEFAULT)
- "EEE MMM dd HH:mm:ss yyyy" (LOCAL)
- Parameters:
dateStr
- the string to be parsednow
- the base date which is used for the calculation of relative formats. E.g. if baseDate is "25.8.2012" then parsing of the string "1 week ago" would result in a date corresponding to "18.8.2012". This is used when a JGit command calls this parser often but wants a consistent starting point for calls.
If set tonull
then the current time will be used instead.- Returns:
- the parsed
Date
- Throws:
java.text.ParseException
- if the given dateStr was not recognized
-
parse
public static java.util.Date parse(java.lang.String dateStr, java.util.Calendar now, java.util.Locale locale) throws java.text.ParseException
Parses a string into aDate
using the given locale. Since this parser also supports relative formats (e.g. "yesterday") the caller can specify the reference date. These types of strings can be parsed:- "never"
- "now"
- "yesterday"
- "(x) years|months|weeks|days|hours|minutes|seconds ago"
Multiple specs can be combined like in "2 weeks 3 days ago". Instead of ' ' one can use '.' to separate the words - "yyyy-MM-dd HH:mm:ss Z" (ISO)
- "EEE, dd MMM yyyy HH:mm:ss Z" (RFC)
- "yyyy-MM-dd"
- "yyyy.MM.dd"
- "MM/dd/yyyy",
- "dd.MM.yyyy"
- "EEE MMM dd HH:mm:ss yyyy Z" (DEFAULT)
- "EEE MMM dd HH:mm:ss yyyy" (LOCAL)
- Parameters:
dateStr
- the string to be parsednow
- the base date which is used for the calculation of relative formats. E.g. if baseDate is "25.8.2012" then parsing of the string "1 week ago" would result in a date corresponding to "18.8.2012". This is used when a JGit command calls this parser often but wants a consistent starting point for calls.
If set tonull
then the current time will be used instead.locale
- locale to be used to parse the date string- Returns:
- the parsed
Date
- Throws:
java.text.ParseException
- if the given dateStr was not recognized- Since:
- 3.2
-
parse_simple
private static java.util.Date parse_simple(java.lang.String dateStr, GitDateParser.ParseableSimpleDateFormat f, java.util.Locale locale) throws java.text.ParseException
- Throws:
java.text.ParseException
-
parse_relative
private static java.util.Date parse_relative(java.lang.String dateStr, java.util.Calendar now)
-
-