Converting String to SQL Date - java

I am trying to convert the String to java.sql.Date.
This string I am getting from my jsp page using request.getparameter(date); which will return string and now I am trying to convert this String to Java.util.Date and then converting to java.SQL.Date.
For this I am using the below code
DateFormat format=new SimpleDateFormat("MM-dd-yyyy");
java.util.Date parsed = new Date(0);
try {
System.out.println("inside try block");
format.setLenient(false);
parsed = format.parse(dob);
System.out.println("parsed date inside try block"+parsed);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But the statement parsed=format.parse(dob) is not executing.

Try the below code
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Source

After the line parsed = format.parse(dob);
you have to add the below line to convert it to java.sql.date format.
java.sql.Date sqlDate = new java.sql.Date(parsed.getTime());
If the string is like "12201430" then you have to use formatter like new SimpleDateFormat("MMyyyydd"),same way for other scenarios also else you will get a ParseException.

You can get this done by using this sample code
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
nowdate="12-30-2014"
Date YourResult=sdf.parse(nowdate);
java.sql.Date todayssqldate=new java.sql.Date(YourResult.getTime());

Related

Formatted text field to take only time in the format '00:00:00'

I am trying to write pre initial code to set formatted time and another to take date in the format date, I am doing Human resource application and I want to calculate over time based on attendance using MySQL database.
try {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(" HH:mm:ss");
String dateString = formatter.format(date);
jFormattedTextField1 = new JFormattedTextField(createFormatter(" ##:##:##"));
} catch (Exception ex) {
}
You can use a SimpleDateFormat the set the text of the textfield with a time
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateString = formatter.format(date);
formatText = new JFormattedTextField(createFormatter("##:##:##"));
formatText.setColumns(8);
formatText.setText(dateString);
If you need to get the date value of the input text, just parse it.
Date date;
try {
date = formatter.parse(formatText.getText());
} catch (ParseException ex) {
...
}
try {
ft = new JFormattedTextField (new MaskFormatter ("##:##:##"));
}
catch (java.text.ParseException e){
}
Navigated to code and set the pre initial code as the one above

Converting date from 20160729161455 to 29/07/2016 16:14:55

How can i convert 20160729161455 to 29/07/2016 16:14:55?
I've tried the following but it did not work:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String dateID = 20160729161455;
try
{
Date date = sdf.parse(dateID);
System.out.println(date);
} catch (ParseException ex){}
Yes you can do. You need 2 SimpeDateFormat objects:
SimpleDateFormat dateParser= new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String dateID = "20160729161455";
try
{
Date date = dateParser.parse(dateID);
System.out.println(dateFormatter.format(date));
} catch (ParseException ex){
ex.printStacktrace();
}
In first step you have to parse the value and in second you have to format it in the new format.

How to formate sql date to Simple date formate in java,android?

java.sql.Date date = java.sql.Date.valueOf(
jObject.getString(AppConstants.KEY_COMMENT_DATE)
);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMM YYYYY");
try {
simpleDateFormat.parse(
jObject.getString(AppConstants.KEY_COMMENT_DATE)
);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am having date in 2015-03-05 03:20:13 format from webservice, I want to format and separate out month, year and other all.
Try following
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
java.util.Date date = df.parse("2015-03-05 03:20:13");

error on converting string to date in java

2014-04-23T18:30:00.000Z need to convert in this format yyyy-MM-dd'T'hh:mm:sszzz in java
I am using this
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.zzzZ");
Date newfromDate = new Date();
try {
//Convert into date
newfromDate = (Date)formatter.parse(fromDate);
// get required Format of date in string format
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:sszzz");
} catch (ParseException e) {
e.printStackTrace();
}
but this is not working
error :- java.text.ParseException: Unparseable date: "2014-03-31T18:30:00.000Z"
try this
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").parse(s);
or this
javax.xml.bind.DatatypeConverter.parseDateTime("2014-04-23T18:30:00.000Z");
the last version is supposed to be faster (no need to parse pattern) and thread-safe

I want to parse String into java date object [duplicate]

This question already has answers here:
convert string into date format
(5 answers)
Closed 9 years ago.
Hello i have date in this format 2013-10-31T19:00:00Z now i want to display this date into yyyy/mm/dd this format.i have used this code but its giving me exception kindly help me
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = df.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
Try this
String DateStr="2013-10-31T19:00:00Z";
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
Date d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
System.out.println(sim.format(d));
output 2013-10-31
That's because you're specifically telling Java to look for a 4-digit year followed by a 2-digit month and 2-digit date. Taken straight from the docs, what you want is
"yyyy-MM-dd'T'HH:mm:ssZ"
You can use SimpleDateFormat's parse method to parse your date, and then use the format method to output it in the format that you prefer.
Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = inputFormat.parse(startDateString);
System.out.println("Date is " + outputFormat.format(date));
Output:
Date is 2013/10/31
You need a separate SimpleDateFormat to parse the Datestring. Try this:
String startDateString = "2013-10-31T19:00:00Z";
DateFormat originalFormat = new SimpleDateFormat("yyyy-mm-dd'T'HH:MM:SS'Z'");
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
Date startDate;
try {
startDate = originalFormat.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is " + newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
You need two different DateFormat. one for parse date and one for formatting date.
String startDateString = "2013-10-31T19:00:00Z";
DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS'Z'");
Date startDate;
try {
startDate =df1.parse(startDateString);
String newDateString = df.format(startDate);
System.out.println("Date is "+newDateString);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Here is simple example
Try this:
try {
String startDateString = "2013-10-31T19:00:00Z";
DateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
DateFormat output = new SimpleDateFormat("yyyy/MM/dd");
Date date = input.parse(startDateString);
System.out.println("Date is " + output.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Pointing an another simple mistake in your code
You coded as DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
here mm is for minutes , for month use MM

Categories