This question already has answers here:
Parse String date in (yyyy-MM-dd) format
(5 answers)
Closed 7 years ago.
I have a Java application that reads information about a new user to insert into an SQL database. When the user is to input a new user's date of birth, I want to program to be sure that the user has entered a date that is in the exact format YYYY-MM-DD.
I want to use the Scanner method next(Pattern pattern), but I'm not sure exactly how to create the necessary Pattern for what I'm trying to achieve. Here is what I have so far:
String date [] = null;
while (date == null) {
try {
date = userInput.next(Pattern.compile(*your suggestion here*)).split("-");
} catch (NoSuchElementException e) {
System.out.print("Date must be in format YYYY-MM-DD: ");
date = null;
userInput.nextLine();
continue;
}
}
What should I put in *your suggestion here*?
Use SimpleDateFormat as a date parser, not a regex.
Related
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 7 years ago.
Hi is there a way to check if an input is a date in Java, the date format is DD/MM/YYYY so int\int\int
I have the basic check if (x != "") but I was wondering if you check that it is a date.
Now I'me getting a new error code:
try {
date = (Date) dateFormat.parse(dateInput);
} catch (ParseException e1) {
System.out.println("FAIL!!!!!!!!");
JOptionPane.showMessageDialog(null, "Date entered is incorrect, please close this window and try again");
new BookApp();
}
The indents probably got messed in when I copied and pasted it, but when the format is wrong it works and the dialog menu pops up. But when I give 12\08\2015 it give a ClassCastError.
How do I get this working?
1) Create a formatter for your date pattern :
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
2) Try to format your String input to a date :
Date date = dateFormat.parse(input);
Now, if the input doesn't match your pattern, you get a ParseException.
You can always try to parse the date and capture if the parse was successful or not.
The SimpleDateFormat will throw an exception if the string was not parsable as a date.
public boolean isDate(String dateString) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
return dateFormat.parse(dateString) != null;
} catch (ParseException e) {
return false;
}
}
This question already has an answer here:
How to convert String to Date in java [duplicate]
(1 answer)
Closed 7 years ago.
I am converting current date into specific format using following line:
new SimpleDateFormat("yyyyMMdd").format(new Date())
This line returns String. How can I convert this string back to date. I want to use the formatted date in SQL query for comparison with date field.
One line java code is expected as I want to make use of it in jasper report in expression field.
use parse method of SimpleDateFormat for that.
new SimpleDateFormat("yyyyMMdd").parse(<string>)
for more info please check official documentation :
https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
You can use parse(String source) method form SimpleDateFormat which takes string and converts it to date like this:
String format = new SimpleDateFormat("yyyyMMdd").format(new Date());
Date parse = new SimpleDateFormat("yyyyMMdd").parse( format );
What do you understand by Formatting ? it's simply the way in which something is arranged or set out. If i say format the document i don't actually change the type of the document i change how it's organised to the way how i want.
While parsing is changing some kind of data into another kind of data.
so here according to your requirement you are changing string to date so first you need to parse and then format it according to your desired format.
One way to do it
Scanner sc=new Scanner(System.in);
System.out.println("Enter the date");
String stringDate=sc.nextLine();
DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");//give it your desired format
Date date=new Date();
try {
date=dateFormat.parse(stringDate);
} catch (ParseException e) {
e.printStackTrace();
}
Edit: Since in comment you says.. you want current date in to be formatted in one line try this it will work.
import java.text.SimpleDateFormat;
import java.util.Date;
public class IntermTest {
public static void main(String...strings ){
System.out.println((new SimpleDateFormat("yyyyMMdd")).format(new Date()));
}
}
This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 8 years ago.
i am making a university project assignment and i am trying to calculate the days between two dates, but so far i did not achieved anything.
The problem is:
I receive two strings in this format "NNNN#AAAA-MM-DD" and i need to check if the dates are alright and all of that. I already made the methods to do it and they are working. The problem is that i cant figure it out how to see the days between without the use of calendar method or date or any type of those ones.
Thanks ;)
you can use this method for convert the string in date:
public Date convertStringToDate(String dateString)
{
Date date = null;
Date formatteddate = null;
DateFormat df = new SimpleDateFormat("NNNN#yyyy-MM-dd");
try{
date = df.parse(dateString);
formatteddate = df.format(date);
}
catch ( Exception ex ){
return null;
}
return formatteddate;
}
This question already has answers here:
Comparing date strings in Java [duplicate]
(6 answers)
Closed 8 years ago.
I am getting date and time which is stored in a String.Below is the date and time which is stored in string.
String userDateTime = "26-Aug-2014 09.00.00 AM";
I have to compare current date and time with the one which is stored in String and if date or time is past date or time or equals to current date and time, it has to perform some logic. Please suggest how can i compare current date and time which is stored in String with the system date and time.
I can use java.util.Date but not sure how can i compare with string format.Please suggest.
First convert the string to date using below way:
SimpleDateFormat ss = new SimpleDateFormat("dd-MMM-yyyy HH.mm.ss a");
String dateInString = "26-Aug-2014 09.00.00 AM";
try {
Date date = ss.parse(dateInString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hen you can compare the dates
You need to parse it first, then compare as a Date (or Joda DateTime) - not as String. There is no way you can achieve that with comparing Strings unless you implement some complicated logic using regex.
You can do something like this:
String string = "26-Aug-2014 09.00.00 AM";
Date date = new SimpleDateFormat("d-MMMM-yyyy", Locale.ENGLISH).parse(string);
See this answer from BalusC:
Java string to date conversion
Then you can compare them with date.compareTo(otherDate)
This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 8 years ago.
I want to transform a String that looks like an sql Timestamp into an actual Timestamp. How can I do that? My string looks like this.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter StartTime (YYYY-MM-DD HH:MM:SS): ");
String StartTime = null;
try {
StartTime = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
Then, I want to compare this timestamp, with a Timestamp from an sql database..Can I do something like that?
Thanks in advance!
Use the Timestamp.valueOf to convert it to a java.sql.Timestamp
You can simplay use the class SimpleDateFormat.According to documentation :
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.