SimpleDateFormat.parse() accepts the date 003/1/2011 when the format is MM/dd/yyyy. Trying with code below:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date dt2;
try
{
dt2 = sdf.parse(_datemmddyyyy);
}
catch (ParseException e)
{
return false;
}
and the date is parsed as 00/11/2011. What is wrong?
Are you sure? This:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
Date dt2 = sdf.parse("003/1/2011");
System.out.println(dt2);
Yields:
Tue Mar 01 00:00:00 PST 2011
That's 03/01/2001 in MM/dd/yyyy. Seems right?
Related
Unable to parse date 02-Mar-00.
Format is -> dd-MMM-yyyy
DateFormat df = new SimpleDateFormat(dateFormat);
df.setLenient(false);
date = df.parse(dateString);
The error message -
Unparseable date: "02-Mar-00"
java.text.ParseException: Unparseable date: "02-Mar-00"
at java.base/java.text.DateFormat.parse(DateFormat.java:388)
at MyClass.main(MyClass.java:9)
As mentioned in the comments above, this won't work until you match your request to the format that you want to parse to!
String dateFormat = "dd-MMM-yy";
String dateString = "02-Mar-00";
DateFormat df = new SimpleDateFormat(dateFormat);
df.setLenient(false);
try {
Date date = df.parse(dateString);
System.out.println(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
This parses the input string to the following output!
Input: 02-Mar-00
Output: Thu Mar 02 00:00:00 IST 2000
Hope this answers your question well!
first you have to parse the date as #N00b Pr0grammer posted
and then format it again in the desired format. I have made some modifications to it, just check it out
String dateFormat = "dd-MMM-yy";
String dateString = "02-Mar-00";
String newFormat = "dd-MMM-yyyy";
DateFormat df = new SimpleDateFormat(dateFormat);
DateFormat displayFormat = new SimpleDateFormat(dateFormat);
df.setLenient(false);
try {
Date date = df.parse(dateString);
System.out.println(displayFormat.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
I want to convert the system date to yyyy-MM-dd format. There are similar questions in SO. I found that I need to parse the date in input format and then convert to the output format. But I am stuck at the first stage itself. I am not able to parse the system date as such (Sat Apr 25 14:44:15 IST 2015).
Here is my MWE:
import java.util.*;
import java.text.*;
public class Test
{
public static void main(String[] args)
{
try
{
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY");
date = dateFormat.parse(date.toString());
System.out.println(date);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
I get the exception as :
Unparseable date: "Sat Apr 25 14:53:33 IST 2015"
Date object can be converted to string of any date format.
String can be converted to date but it will come only in standard date format's but cant be in the one as you want..
If you want to format system date to yyyy-MM-dd format then use:
Date date = new Date();
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
String date1 = dateFormater.format(date);
As you specified in comment you want to subtract sql date with current date then just convert the sql date to normal date format.
Like this:
String date = your date;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = dateFormat.parse(date);
Date currentdate = new Date();
Then use calender objects:
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
Calendar calendar2 = calendar.getInstance();
calendar2.setTime(currentdate);
long difference = (calendar2.getTimeInMillis() - calendar
.getTimeInMillis()) / 60000;
This will give you the difference between two dates in minutes.
This will work for you
public class Test1 {
public static void main(String[] args) {
try
{
Date date = new Date();
System.out.println(date);
String dateFormat = new SimpleDateFormat("EEE MM dd HH:mm:ss aaa YYYY").format(date);
System.out.println(dateFormat);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Output
Sat Apr 25 15:10:38 IST 2015
Sat 04 25 15:10:38 PM 2015
I think you should do it like that.
Date date = new Date();
String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
date = format.parse(formattedDate);
System.out.println(date);
But you should understand difference between "date" and "date format".
How to format a string that looks like this
Sat Dec 08 00:00:00 JST 2012
into yyyy-mm-dd i.e.
2012-12-08
From browsing the web, I found this piece of code:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "Sat Dec 08 00:00:00 JST 2012";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
However, I am unable to modify it to accept the first line (Sat Dec 08 00:00:00 JST 2012) as a string and format that into the yyyy-mm-dd format.
What should I do about this? Should I be attempting to modify this? Or try another approach altogether?
Update: I'm using this from your answers (getting error: Unparseable date: "Sat Dec 08 00:00:00 JST 2012")
public static void main(String[] args) throws ParseException{
SimpleDateFormat srcFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.JAPANESE);
SimpleDateFormat destFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.JAPANESE);
Date date = srcFormatter.parse("Sat Dec 08 00:00:00 JST 2012");
String destDateString = destFormatter.format(date);
/* String dateInString = "Sat Dec 08 00:00:00 JST 2012";*/
System.out.println(destDateString);
/*try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}*/
}
}
Try this -
SimpleDateFormat srcFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.JAPANESE);
SimpleDateFormat destFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.JAPANESE);
Date date = srcFormatter.parse("Sat Dec 08 00:00:00 JST 2012");
String destDateString = destFormatter.format(date);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String dateInString = "Wed Oct 16 00:00:00 CEST 2013";
try {
SimpleDateFormat parse = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date date = parse.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Change your formation to this new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Thanks..
You need two SimpleDateFormat objects. One to parse the date from the string using parse() method and the second one to output it in desired format using format() method. For more info about date formatting check the docs.
im getting the SQL Server datetime (1 Jan 2013) form the SqlRowSet like
while (rs.next()) {
myBean.setDateProp(rs.getString(4));
}
the type of myBean DateProp is java.util.Date, is there a way to convert (1 Jan 2013) to java Date representation.
i have tried the following code
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
Date date=new Date();
try {
date = sdf.parse("1 Jan 2013");
} catch (Exception e) {
e.printStackTrace();
}
and i get the ParseException
SEVERE: java.text.ParseException: Unparseable date: "1 Jan 2013"
any directions...
try this -
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
Date date=new Date();
try {
date = sdf.parse("1 Jan 2013");
}catch (Exception e) {
e.printStackTrace();
}
Try
while (rs.next()) {
myBean.setDateProp(rs.getDate(4));
}
#Test
public void test() throws ParseException {
String dateString = "1 Jan 2013";
String dateString2 = "11 Jan 2013";
SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy");
sdf.parse(dateString);
sdf.parse(dateString2);
}
You need to modify your Date formatter string to
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
as your date string is in this format
I am trying to format a date by parsing it and then formating it but it is not working.
It is showing a parsing exception
public java.util.Date convertFormat(String DateTimeForm)
throws ParseException {
DateTimeForm="2012-06-01 10:00 PM";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
java.util.Date FCDate = (java.util.Date) formatter.parse(DateTimeForm);
return (java.util.Date) FCDate;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
try {
Date date = formatter.parse("2012-06-01 10:00 PM");
System.out.println(date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
Didn't change anything and yet it works.
Fri Jun 01 22:00:00 CDT 2012
This works fine on my machine. I didn't change anything important.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
Date date = null;
try {
date = formatter.parse("2012-06-01 10:00 PM");
} catch (ParseException ex) {
// Intentionally empty. Failed parse causes date == null.
}
System.out.print(date);
prints
Fri Jun 01 22:00:00 EDT 2012
The Java docs say the numerics are all locale-independent, but not the AM/PM. For example the code fails if you specify Locale.JAPAN in the formatter construction. Specify Local.US to guarantee AM/PM will always work.