Code for matching date Pattern [duplicate] - java

This question already has answers here:
Java: Check the date format of current string is according to required format or not [duplicate]
(8 answers)
Closed 6 years ago.
I have a scenario where I get the date value from the xml and the date value from the website. I am able to retrieve the results, But now I need to compare the format of the retrieved results whether it is in 'MM YYYY' format or not ? How can i do that ? i have to check the format in String manner ie for e.g Apr 2010 but not 04 2010
Please can anyone of you help me ??

public class SimpleDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
String strDate = sdf.format(date);
sdf = new SimpleDateFormat("MMM/yyyy");
strDate = sdf.format(date);
System.out.println(strDate);
}
}

You can use a regular expresson, like this (Jan|Feb|Mar...) d{4}

Related

How do I convert date formatted string timestamp into date? [duplicate]

This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
display Java.util.Date in a specific format
(11 answers)
Closed 2 years ago.
I need to convert the current timestamp and current minus 1-minute timestamp into a given format. I was able to convert the desired format. But it was returning as String.
I need the formatted output as Date instead of String.
public static void main(String[] args) throws ParseException
{
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
long now = System.currentTimeMillis();
String toTimeStamp = dateFormatter.format(now);
long nowMinus1Minutes = now - TimeUnit.MINUTES.toMillis(1);
String fromTimeStamp = dateFormatter.format(nowMinus1Minutes);
System.out.println(fromTimeStamp);
System.out.println(toTimeStamp);
// Tried with parse
Date fromDate = dateFormatter.parse(fromTimeStamp);
Date toDate = dateFormatter.parse(toTimeStamp);
System.out.println(fromDate);
System.out.println(toDate);
}
Output:(String)
2020-07-24 12:13:00
2020-07-24 12:12:00
Output: Pose Parsed the string as Date
Fri Jul 24 12:16:00 IST 2020
Fri Jul 24 12:17:00 IST 2020
Expected Output:
2020-07-24 12:13:00 (As Date Object)
2020-07-24 12:12:00 (As Date Object)
Can't be possible. Understood lately. Closing this.
Once you transform your String into a Date, the initial format you used, whatever it is is lost.
If you want to display your data in the "yyyy-MM-dd HH:mm:00", use your formatter again otherwise Date class will stick to its default format:
System.out.println(formatter.format(fromDate));
System.out.println(formatter.format(toDate));
If you need two Date-Object, you could do it like this:
OffsetDateTime offsetDateTime1 = OffsetDateTime.now();
OffsetDateTime offsetDateTime2 = offsetDateTime1.minusMinutes(1);
System.out.println(Date.from(offsetDateTime1.toInstant()));
System.out.println(Date.from(offsetDateTime2.toInstant()));
If you need the timestamp to be printed in other format, you can use SimpleDateFormat on the Date-Objects afterwards.

Simple Date format returns Wrong date intermittently [duplicate]

This question already has answers here:
Why is Java's SimpleDateFormat not thread-safe? [duplicate]
(9 answers)
Closed 4 years ago.
I am converting Date to string format in yyyy-MM-dd HH:mm:ss format to save in sqlite database
below is object declared for simple date format
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Sometime it prepends pair of zeros in date as you can see in below image
Example Wrong date returns as as below
2018-08-25 02:32:0000
2018-08-25 02:32:0049
2018-0008-25 02:32:50
2018-08-24 0023:32:50
2018-08-0024 23:32:50
I have created custom funtion to correct this wrong date. But I want to know exact cause of this issue.
below is the code
public static String getCurrentDateTime() {
Date d = new Date();
String datetime = sdf.format(d);
if (datetime.length() > 19) {
datetime = correctDate(datetime);
}
return datetime;
}
I'm sure that if you don't use that static instance of SimpleDateFormat you will have no problem:
public static String getCurrentDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
String datetime = sdf.format(d);
return datetime;
}
See these links:
Why is Java's SimpleDateFormat not thread-safe?
"Java DateFormat is not threadsafe" what does this leads to?
SimpleDateFormat is not thread safe that's why you should check if in your calling code there is no issue with that.

JAVA - Format a custom string using SimpleDateFormat [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Java string to date conversion
(17 answers)
Closed 5 years ago.
I'm having trouble formatting a custom String back to a Date object. What i have:
String customString = "October 14, 2015;
Date date = new Date();
SimpleDateFormat s = new SimpleDateFormat("MM-dd-yyyy");
try {
date = s.parse(customString);
} catch (ParseException e) {
e.printStackTrace();
}
I always get a unappeasable date exception. Any pointers of what i'm doing wrong is appreciated.
Your pattern must be: new SimpleDateFormat("MMM dd,yyyy");
For more informations about SimpleDateFormat see the javadoc
Read the docs, https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
The argument of the constructor should have the format of the date you want to input.
For instance, if you want the full month name should have "MMMMM".
Just make the following change and the program will work.
SimpleDateFormat s = new SimpleDateFormat("MMMMM dd, yyyy");

How to get year, month, day from a String format in Java [duplicate]

This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Closed 6 years ago.
I am working on a piece of work where I need to get year, month, day from a String in Java. The string is like that: 20170119T163048
Where 2017 is year, 01 is month, 19 is day, 16:30:48 is time.
I implemented code like this below:
public void convertStringToDate (String string) {
String dateInString = "";
SimpleDateFormat formatter = new SimpleDateFormat();
Date date = formatter.parse("20170119T163048");
System.out.println(date);
}
But seems like it does not work. Looking around and I actually found quite lots of similar answer but none of them really work for me so...
You Shall always refer to the documentation.Simple Date Format Documentation
public static void main(String[] args) throws Exception{
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
Date date = formatter.parse("20170119T163048");
System.out.println(date.toString());
}

java - Convert date string to UTC time [duplicate]

This question already has answers here:
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 6 years ago.
I just needed a sample code block or suggestion to convert the following date string to utc time and find difference with current time in java?
date string "2016-03-21T15:58:36-04:00"
Thanks in advance
You need to format the date string in following way:
String string = "2016-03-21T15:58:36-04:00";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
Date date = format.parse(string);
Then you just need to use Date APIs to find the time difference.
public static long getMillisFrom(String inStrDate) {
DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);
Date inDate = ISO_DATE_FORMAT.parse(inStrDate);
Date curDate = new Date();
long diffMillis = curDate().getTime() - inDate.getTime();
return diffMillis
}

Categories