to determine whether the any string is a datetime use java [duplicate] - java

I know this question is asked quite a bit, and obviously you can't parse any arbitrary date. However, I find that the python-dateutil library is able to parse every date I throw at it, all while requiring absolutely zero effort in figuring out a date format string. Joda time is always sold as being a great Java date parser, but it still requires you to decide what format your date is in before you pick a Format (or create your own). You can't just call DateFormatter.parse(mydate) and magically get a Date object back.
For example, the date "Wed Mar 04 05:09:06 GMT-06:00 2009" is properly parsed with python-dateutil:
import dateutil.parser
print dateutil.parser.parse('Wed Mar 04 05:09:06 GMT-06:00 2009')
but the following Joda time call doesn't work:
String date = "Wed Mar 04 05:09:06 GMT-06:00 2009";
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
DateTime dt = fmt.parseDateTime(date);
System.out.println(date);
And creating your own DateTimeFormatter defeats the purpose, since that seems to be the same as using SimpleDateFormatter with the correct format string.
Is there a comparable way to parse a date in Java, like python-dateutil? I don't care about errors, I just want it to mostly perfect.

Your best bet is really asking help to regex to match the date format pattern and/or to do brute forcing.
Several years ago I wrote a little silly DateUtil class which did the job. Here's an extract of relevance:
private static final Map<String, String> DATE_FORMAT_REGEXPS = new HashMap<String, String>() {{
put("^\\d{8}$", "yyyyMMdd");
put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy");
put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd");
put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy");
put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}$", "dd MMM yyyy");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}$", "dd MMMM yyyy");
put("^\\d{12}$", "yyyyMMddHHmm");
put("^\\d{8}\\s\\d{4}$", "yyyyMMdd HHmm");
put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}$", "dd-MM-yyyy HH:mm");
put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy-MM-dd HH:mm");
put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}$", "MM/dd/yyyy HH:mm");
put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy/MM/dd HH:mm");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMM yyyy HH:mm");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMMM yyyy HH:mm");
put("^\\d{14}$", "yyyyMMddHHmmss");
put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss");
put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd-MM-yyyy HH:mm:ss");
put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss");
put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "MM/dd/yyyy HH:mm:ss");
put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy/MM/dd HH:mm:ss");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMM yyyy HH:mm:ss");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMMM yyyy HH:mm:ss");
}};
/**
* Determine SimpleDateFormat pattern matching with the given date string. Returns null if
* format is unknown. You can simply extend DateUtil with more formats if needed.
* #param dateString The date string to determine the SimpleDateFormat pattern for.
* #return The matching SimpleDateFormat pattern, or null if format is unknown.
* #see SimpleDateFormat
*/
public static String determineDateFormat(String dateString) {
for (String regexp : DATE_FORMAT_REGEXPS.keySet()) {
if (dateString.toLowerCase().matches(regexp)) {
return DATE_FORMAT_REGEXPS.get(regexp);
}
}
return null; // Unknown format.
}
(cough, double brace initialization, cough, it was just to get it all to fit in 100 char max length ;) )
You can easily expand it yourself with new regex and dateformat patterns.

There is a nice library called Natty which I think fits your purposes:
Natty is a natural language date parser written in Java. Given a date
expression, natty will apply standard language recognition and translation
techniques to produce a list of corresponding dates with optional parse and
syntax information.
You can also try it online!

You could try dateparser.
It can recognize any String automatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTime correctly and quickly(1us~1.5us).
It doesn't based on any natural language analyzer or SimpleDateFormat or regex.Pattern.
With it, you don't have to prepare any appropriate patterns like yyyy-MM-dd'T'HH:mm:ss.SSSZ or yyyy-MM-dd'T'HH:mm:ss.SSSZZ:
Date date = DateParserUtils.parseDate("2015-04-29T10:15:00.500+0000");
Calendar calendar = DateParserUtils.parseCalendar("2015-04-29T10:15:00.500Z");
LocalDateTime dateTime = DateParserUtils.parseDateTime("2015-04-29 10:15:00.500 +00:00");
All works fine, please enjoy it.

What I have seen done is a Date util class that contains several typical date formats. So, when DateUtil.parse(date) is called, it tries to parse the date with each date format internally and only throws exceptions if none of the internal formats can parse it.
It is basically a brute force approach to your problem.

//download library: org.ocpsoft.prettytime.nlp.PrettyTimeParser
String str = "2020.03.03";
Date date = new PrettyTimeParser().parseSyntax(str).get(0).getDates().get(0);
System.out.println(date)

I have no idea about this parsing how to do in python. In java we can do like this
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date normalDate = null;
java.sql.Date sqlDate = null;
normalDate = sdf1.parse(date);
sqlDate = new java.sql.Date(normalDate.getTime());
System.out.println(sqlDate);
i think like java some predefined functions will be there in python. You can follow this method.
This methods parse the String date to Sql Date (dd-MM-yyyy);
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class HelloWorld{
public static void main(String []args){
String date ="26-12-2019";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date normalDate = null;
java.sql.Date sqlDate = null;
if( !date.isEmpty()) {
try {
normalDate = sdf1.parse(date);
sqlDate = new java.sql.Date(normalDate.getTime());
System.out.println(sqlDate);
} catch (ParseException e) {
}
}
}
}
execute this!

Related

Create a function that takes string object and returns Date object in dd-MM-yyyy format [duplicate]

This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 6 years ago.
I have date in string object. I want to convert into Date object.
Date getDateFmString(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(dateString);
return convertedCurrentDate ;
}
above function returning following output.
Fri Apr 22 00:00:00 IST 2016
but I want output in this format '2016-03-01' only
function should take string only.
function should return Date object.
I have done lot of research over web, but I got solution from one Expert.
Date getDateFrmString(String dDate)
{
java.sql.Date dDate = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(sDate).getTime());
return dDate;
}
this is what I want.
Change the date format from
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
to
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
Hope this works
See this example
public Class DateFormatDemo{
public static void main (String args[]) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
String dateInString = "01/01/2015";
try{
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}catch(ParseException e){
e.printStackTrace();
}
}
}
This link might help you with string to date object conversions
You are parsing with the wrong format try
String dateString="01-01-2016";
SimpleDateFormat sdfP = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdfP .parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
Output:
2016-01-01
DEMO1
And if you want the format to dd-MM-yyyy then no need to define seperate SimpleDateFormat object.
String dateString="01-01-2016";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date convertedCurrentDate = sdf.parse(dateString);
String date=sdf.format(convertedCurrentDate );
System.out.println(date);
OUTPUT:
01-01-2016
DEMO2
To format the string date you have to first parse the String to Date object using the same format of date which the String have then format it using the desired format as seen in the above code.
Date objects don't have a format. Only a String does. A Date object will be output with whatever format you tell it to be format as. It all depends on what the format of the DateFormat object is when you call .format(). Calling the toString() method on a Date object uses a DateFormat of "dow mon dd hh:mm:ss zzz yyyy".
Let's do it step by step:
You have a date as String in dd-MM-yyyy format.
You want to convert it into date. (for this you are using SimpleDateFormat)
Now you are printing the date. Question here is are you printing the converted date object or input string?
If its a date object then toString method is called of date class.
As per comment on java.util.Date class it's:
dow mon dd hh:mm:ss zzz yyyy
similar to
Fri Apr 22 00:00:00 IST 2016
So that coincides with what you get in output in the second approach. But how is that code even running is strange.
String inputStr = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(input);
Variable 'input' is not defined.
What are the possible solutions:
While printing date, convert it back to String using SimpleDateFormat as per the requirement.
Date d =new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dStr = sdf.format(dateString);
System.out.printn(dStr);
Extending class java.util.Date and override toString, but that would be a bad idea.

Parse a date with the timezone "Etc/GMT"

My first attempt was:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date date = formatter.parse(string);
It throws ParseException, so I found this hack:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
TimeZone timeZone = TimeZone.getTimeZone("Etc/GMT");
formatter.setTimeZone(timeZone);
Date date = formatter.parse(string);
It did not work either, and now I'm stuck. It parses without problems if I just change the timezone to "GMT".
edit: An example string to parse would be "2011-11-29 10:40:24 Etc/GMT"
edit2: I would prefer not to remove timezone information completely. I am coding a server that receives the date from an external user, so perhaps other dates will have other timezones.
To be more precise: This specific date I receive is from the receipt from the apple server after making an in app purchase on an iphone app, but I could also receive dates from other sources.
Don't know if this question is still relevant to you, but if you use Joda time, this'll work:
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss ZZZ").parseDateTime(s)
Without Joda time the following will work (bit more work though):
String s = "2011-11-29 10:40:24 Etc/GMT";
// split the input in a date and a timezone part
int lastSpaceIndex = s.lastIndexOf(' ');
String dateString = s.substring(0, lastSpaceIndex);
String timeZoneString = s.substring(lastSpaceIndex + 1);
// convert the timezone to an actual TimeZone object
// and feed that to the formatter
TimeZone zone = TimeZone.getTimeZone(timeZoneString);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(zone);
// parse the timezoneless part
Date date = formatter.parse(dateString);
It didn't work for me either the thing is I tried setting TimeZone of SimpleDateFormatter to "Etc/GMT" and then formatted a new date here is the output:
2011-11-30 10:46:32 GMT+00:00
So Etc/GMT is being translated as GMT+00:00
If you really want to stick to parse "2011-09-02 10:26:35 Etc/GMT" then following will help too without even considering explicit Timezone change:
java.text.SimpleDateFormat isoFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss 'Etc/GMT'");
isoFormat.parse("2010-05-23 09:01:02 Etc/GMT");
Works fine.
Following code is working for me
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
try { System.out.println( sdf.parse("2011-09-02 10:26:35 Etc/GMT") );
} catch (ParseException e){
e.printStackTrace();
}

How can I convert this string to a standard date in java?

Earlier I posted the following question: How can I convert this date in Java?
But now I would like to know how I can convert this string into a date/time.
2010-03-15T16:34:46Z
For example: 03/15/10
UPDATED:
String pattern = "MM/dd/yy 'at' HH:mm";
Date date = new Date();
try {
date = new SimpleDateFormat(pattern).parse(q.getUpdated_at());
} catch (ParseException e) {
e.printStackTrace();
}
dateText.setText(new SimpleDateFormat("MM/dd/yy 'at' hh:mma").format(date));
Gives me a result like:
Mon Mar 15 16:34:50 MST 2010
How can I format it to be
03/15/10 at 4:34PM
?
Both SimpleDateFormat and joda-time DateTimeFormat can parse this, using this pattern:
String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
For example:
Date date = new SimpleDateFormat(pattern).parse(dateString);
And (joda-time):
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(s);
Update
You have 2 date formats involved - one for parsing the input, and one for formatting the output. So:
dateText.setText(new SimpleDateFormat("MM/dd/yy 'at' hh:mma").format(date));
(Of course, for the sake of optimization, you can instantiate the SimpleDateFormat only once, and reuse it)
In a nutshell, you want to convert a date in a string format to a date in another string format. You have:
2010-03-15T16:34:46Z
and you want
03/15/10 at 4:34PM
You don't want to end up using java.util.Date object as you initially implied in your question. You also don't want to use its toString() since that returns a fixed format as definied in its javadoc.
The answer of Bozho still applies. Use java.text.SimpleDateFormat. First, you need to parse the date in string format into a Date object so that you can format it back into another string format.
// First parse string in pattern "yyyy-MM-dd'T'HH:mm:ss'Z'" to date object.
String dateString1 = "2010-03-15T16:34:46Z";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(dateString1);
// Then format date object to string in pattern "MM/dd/yy 'at' h:mma".
String dateString2 = new SimpleDateFormat("MM/dd/yy 'at' h:mma").format(date);
System.out.println(dateString2); // 03/15/10 at 4:34PM
If you want to format output string, change following line in your code
dateText.setText(date.toString());
to
dateText.setText(String.format("%1$tm/%1$td/%1$ty at %1$tl:%1$tM%1$Tp", date));

Any easy ways to unformat a date?

I've got a bunch of dates in this String format:
String date = "Wed Sep 15 16:31:05 BST 2010";
and I'd like to convert it back to a date or calendar object. Before I go and reinvent the wheel, are there any easy ways of doing this, preferably present in the JDK?
Using SimpleDateFormat
String format = "EE MMM dd HH:mm:ss zz yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
Date result = sdf.parse(date);
Alternatively, as suggested by Jon Skeet, you can use JodaTime's DateTimeFormat - the pattern should be the same. But it appears that the BDT/BST/BDST timezone aliases are not supported properly by JodaTime.
Well, that's what java.text.DateFormat is for (and particularly its SimpleDateFormat subclass) - but personally I would suggest that you use Joda Time instead.
In particular Joda Time's DateTimeFormatter class is thread-safe, unlike SimpleDateFormat - so you can create a single instance with the appropriate pattern, and use it from any thread. Additionally, the DateTimeFormat class acts as a factory with lots of preset patterns in ISODateFormat. Oh, and controlling the time zone etc is rather better with Joda Time.
Finally, Joda Time is simply a better date and time API. It's not perfect, but it's much better than the built-in Date and Calendar support in Java.
EDIT: Trying to parse your sample string, I'm having trouble with the "BST" bit... partly because that's not really a full time zone (it's just the DST part of the Europe/London time zone) and partly because I can't quite get Joda Time to do what I want... it looks like in this one case, SimpleDateFormat wins out :(
SimpleDateFormat
public static void main(String[] args) {
try { String str_date="11-June-07";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
System.out.println("Today is " +date );
} catch (ParseException e)
{System.out.println("Exception :"+e); }
}
JodaTime
import org.joda.time.format.*;
import org.joda.time.*;
...
String dateString = "2009-04-17 10:41:33";
// parse the string
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = formatter.parseDateTime(dateString);
// add two hours
dateTime = dateTime.plusHours(2); // easier than mucking about with Calendar and constants
System.out.println(dateTime);

Parse any date in Java

I know this question is asked quite a bit, and obviously you can't parse any arbitrary date. However, I find that the python-dateutil library is able to parse every date I throw at it, all while requiring absolutely zero effort in figuring out a date format string. Joda time is always sold as being a great Java date parser, but it still requires you to decide what format your date is in before you pick a Format (or create your own). You can't just call DateFormatter.parse(mydate) and magically get a Date object back.
For example, the date "Wed Mar 04 05:09:06 GMT-06:00 2009" is properly parsed with python-dateutil:
import dateutil.parser
print dateutil.parser.parse('Wed Mar 04 05:09:06 GMT-06:00 2009')
but the following Joda time call doesn't work:
String date = "Wed Mar 04 05:09:06 GMT-06:00 2009";
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
DateTime dt = fmt.parseDateTime(date);
System.out.println(date);
And creating your own DateTimeFormatter defeats the purpose, since that seems to be the same as using SimpleDateFormatter with the correct format string.
Is there a comparable way to parse a date in Java, like python-dateutil? I don't care about errors, I just want it to mostly perfect.
Your best bet is really asking help to regex to match the date format pattern and/or to do brute forcing.
Several years ago I wrote a little silly DateUtil class which did the job. Here's an extract of relevance:
private static final Map<String, String> DATE_FORMAT_REGEXPS = new HashMap<String, String>() {{
put("^\\d{8}$", "yyyyMMdd");
put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy");
put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd");
put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy");
put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}$", "dd MMM yyyy");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}$", "dd MMMM yyyy");
put("^\\d{12}$", "yyyyMMddHHmm");
put("^\\d{8}\\s\\d{4}$", "yyyyMMdd HHmm");
put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}$", "dd-MM-yyyy HH:mm");
put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy-MM-dd HH:mm");
put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}$", "MM/dd/yyyy HH:mm");
put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy/MM/dd HH:mm");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMM yyyy HH:mm");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMMM yyyy HH:mm");
put("^\\d{14}$", "yyyyMMddHHmmss");
put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss");
put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd-MM-yyyy HH:mm:ss");
put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss");
put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "MM/dd/yyyy HH:mm:ss");
put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy/MM/dd HH:mm:ss");
put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMM yyyy HH:mm:ss");
put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMMM yyyy HH:mm:ss");
}};
/**
* Determine SimpleDateFormat pattern matching with the given date string. Returns null if
* format is unknown. You can simply extend DateUtil with more formats if needed.
* #param dateString The date string to determine the SimpleDateFormat pattern for.
* #return The matching SimpleDateFormat pattern, or null if format is unknown.
* #see SimpleDateFormat
*/
public static String determineDateFormat(String dateString) {
for (String regexp : DATE_FORMAT_REGEXPS.keySet()) {
if (dateString.toLowerCase().matches(regexp)) {
return DATE_FORMAT_REGEXPS.get(regexp);
}
}
return null; // Unknown format.
}
(cough, double brace initialization, cough, it was just to get it all to fit in 100 char max length ;) )
You can easily expand it yourself with new regex and dateformat patterns.
There is a nice library called Natty which I think fits your purposes:
Natty is a natural language date parser written in Java. Given a date
expression, natty will apply standard language recognition and translation
techniques to produce a list of corresponding dates with optional parse and
syntax information.
You can also try it online!
You could try dateparser.
It can recognize any String automatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTime correctly and quickly(1us~1.5us).
It doesn't based on any natural language analyzer or SimpleDateFormat or regex.Pattern.
With it, you don't have to prepare any appropriate patterns like yyyy-MM-dd'T'HH:mm:ss.SSSZ or yyyy-MM-dd'T'HH:mm:ss.SSSZZ:
Date date = DateParserUtils.parseDate("2015-04-29T10:15:00.500+0000");
Calendar calendar = DateParserUtils.parseCalendar("2015-04-29T10:15:00.500Z");
LocalDateTime dateTime = DateParserUtils.parseDateTime("2015-04-29 10:15:00.500 +00:00");
All works fine, please enjoy it.
What I have seen done is a Date util class that contains several typical date formats. So, when DateUtil.parse(date) is called, it tries to parse the date with each date format internally and only throws exceptions if none of the internal formats can parse it.
It is basically a brute force approach to your problem.
//download library: org.ocpsoft.prettytime.nlp.PrettyTimeParser
String str = "2020.03.03";
Date date = new PrettyTimeParser().parseSyntax(str).get(0).getDates().get(0);
System.out.println(date)
I have no idea about this parsing how to do in python. In java we can do like this
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date normalDate = null;
java.sql.Date sqlDate = null;
normalDate = sdf1.parse(date);
sqlDate = new java.sql.Date(normalDate.getTime());
System.out.println(sqlDate);
i think like java some predefined functions will be there in python. You can follow this method.
This methods parse the String date to Sql Date (dd-MM-yyyy);
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class HelloWorld{
public static void main(String []args){
String date ="26-12-2019";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
java.util.Date normalDate = null;
java.sql.Date sqlDate = null;
if( !date.isEmpty()) {
try {
normalDate = sdf1.parse(date);
sqlDate = new java.sql.Date(normalDate.getTime());
System.out.println(sqlDate);
} catch (ParseException e) {
}
}
}
}
execute this!

Categories