How to check the string is a Date [duplicate] - java

This question already has answers here:
Parse any date in Java
(6 answers)
Recognise an arbitrary date string [closed]
(15 answers)
Closed 8 years ago.
I need to check the string against all possible dateFormats and need to check its valid date or not.
The problem I am facing is I can't predict the date format as its getting from some other web sources. One example is September 21 2012 1255 PM ET, .But the format may vary.

I suggest that you implement your own Parse method and use regular expressions to determine how the string should be parsed. You should not expect to parse a date with any format without knowing which it is.
Personally I would implement something like this (but then again, I havenĀ“t coded Java in quite some time):
public SimpleDateFormat ParseDate(string s)
{
SimpleDateFormat parsedDate;
if(s.matches("0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d"))
parsedDate = new SimpleDateFormat("MM/dd/yyyy");
else if(s.matches([PATTERN2]))
parsedDate = new SimpleDateFormat([DATE PATTERN 2]);
...
return parsedDate;
}
and so on...
Suggested reading:
http://docs.oracle.com/javase/tutorial/essential/regex/
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Related

Converting String date/time values [duplicate]

This question already has answers here:
How can I convert a date in YYYYMMDDHHMMSS format to epoch or unix time?
(2 answers)
Changing String date format
(4 answers)
Change date format in a Java string
(22 answers)
Closed 3 years ago.
I am currently facing a problem when it comes to some Java methods that weren't explained to me in lectures very well. I need to write a program that accepts user-inputted strings (particularly a date) in yyyymmddhhss format, which should then convert to hh:mm Month day, year.
E.g. 201901151500 outputs: "03:00 PM January 15, 2019".
Currently, in my program, I have accepted the user's input and implemented a method that returns an error message if the inputted format is invalid.
Any tips on where to go from here? Advice is greatly appreciated-- thank you!
If you are using Java 8 you can use java.time API like so :
String input = "201901151500";
LocalDateTime dt = LocalDateTime.parse(input, DateTimeFormatter.ofPattern("uuuuMMddHHmm"));
String output = dt.format(DateTimeFormatter.ofPattern("hh:mm a MMMM dd, uuuu"));
>> output = 03:00 PM janvier 15, 2019
Use the DateTimeFormatter as defined here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Pay attention to the parse method. You can define a formatter that takes in a string and then returns it in a certain way, almost any way you choose.
Here is the LocalDataTime class:
https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
Example code:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
LocalDateTime local = LocalDateTime.parse("2004 12 25", formatter);

DateFormatter parse String to Date gives wrong format [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Change the format of Date Java [closed]
(2 answers)
Closed 4 years ago.
I have the following piece of code:
System.out.println(array[9]);
Date d = df.parse(array[9]);
System.out.println(d.toString());
and the result of this looks like the following:
01.01.2017
Sun Jan 01 00:00:00 CET 2017
My DateFormatter:
DateFormat df = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);
So my question is now why I get the wrong format.
First result is a string, which I must convert to date.
But I got the wrong format, not the German one (dd.MM.yyyy).
What's wrong?
In your example you should use df.format(d) if you plan to convert Date to String. The default Date.toString() method will use the predefined format which you can't control.

Parsing weird date format [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I have this weird date format that I have to parse.
2015-12-18T03:36:06.000+0000
I am currently mapping a regex to date formats so I can parse different dates. However, this format got me confused. Any help appreciated.
To parse a String into a Date in Java, you use a DateFormat object, and specify the format the date is in. There is no need to use a Regex, the Java library has a way to do this for you.
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
final Date d = df.parse("2015-12-18T03:36:06.000+0000"); // From your code above
System.out.println(d);
See the JavaDoc for SimpleDateFormat for more explanation as to what the symbols mean. This is actually a common format for dates called ISO 8601, I just took the pattern right from the documentation.
Watch out! These DateFormat objects are not threadsafe.

Formating date in desired format as Date not as string [duplicate]

This question already has answers here:
Java new Date() in print
(7 answers)
Closed 8 years ago.
If i convert the string to date using simple date format..
SimpleDateFormat parser=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(parser.parse("2014-11-24"));
It return the value as Mon Nov 24 00:00:00 GMT+05:30 2014
I want it should be in format "yyyy-MM-dd" . If i use SimpleDateFormat format() method. It returns the value as string but i want the value should be in Date. not as a string. Is there any way to do this?
Date != String
Your call to SimpleDateFormat.parse() returns a Date object, then you call System.out.println() which defaults to using the toString() of Object. The Date objects .toString() gets a String representation using its default format.
Why do you expect something different than what the JavaDoc clearly explains? Date.toString() is well documented.
If you want to print it out back in the format it came from that is what the SimpleDateFormat.format() method is for. But that would give you the value that you already parsed, so what you are trying to do demonstrates a fundamental lack of comprehension of Java and how to read JavaDoc and other basic fundamental skills required to program in the Java language.
If you took the time it took you to post this question to read the JavaDoc on the objects and methods on those objects this is explained in great detail!

Convert epoch String to Java date [duplicate]

This question already has answers here:
Unix epoch time to Java Date object
(7 answers)
Closed 9 years ago.
I am getting an epoch String from my DB which looks something like this : 1391328000000
I am having a hard time trying to convert it to Java Date.
I tried the following :
private String buildDate(String dateString){
System.out.println("dateString " + dateString);
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = format.format(Integer.parseInt(dateString));
return formatted;
}
I think you're overthinking about the DateFormat. If I want to simply obtain a Date instance, what I would try is the following:
Date d = new Date(Long.parseLong(dateString));
You need to turn it into a java.util.Date object in order for SimpleDateFormat to handle it. Also, a value like what you quoted needs to be parsed as a long, as it is too large for an int.
That is, change the line where you set formatted to be:
String formatted = format.format(new Date(Long.parseLong(dateString)));
As an aside, if the project you're working on can handle an extra external dependency, switch date/time handling over to the joda library. The stuff in java.util (that is, Date and Calendar) rapidly becomes painful and error-prone to work with.

Categories

Resources