I have written following program to convert date string to sql date object to store in db2 but the ouput shown is 2013-01-02 instead of 2013-02-02. can anyone explain why??
String string = "02/02/2013";
Date date = null;
try {
date = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH).parse(string);
java.sql.Date newDate=new java.sql.Date(date.getTime());
System.out.println(newDate);
} catch (ParseException e) {
e.printStackTrace();
}
For month you should use MM instead of mm. mm is used for minutes in hour: -
date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
your format should be
"dd/MM/yyyy"
note that mm is for minutes whereas MM is for Month
Check the Doc
Related
This question already has answers here:
Getting wrong month when using SimpleDateFormat.parse
(3 answers)
Closed 5 years ago.
I am trying to convert string of format yyyy-mm-dd to dd-MMM-yy. I am getting correct year and days but for month it is showing only jan irrespective of my input. How to fix it?
String input = "2013-09-14";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MMM-yy");
Date date = null;
try {
date = format1.parse(input);
String temp = format2.format(date);
System.out.println(temp);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Output:
14-Jan-13
But I should get:
14-Oct-13
mm is for minute
you need MM or MMM for month.
See SimpleDateFormat for reference.
mm is for minutes. MM is for months.
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
I would suggest latest API LocalDate which not requires try/catch and which is easier to use :
String input = "2013-09-14";
LocalDate inputDate = LocalDate.parse(input, DateTimeFormatter.ISO_DATE);
String format = inputDate.format(DateTimeFormatter.ofPattern("dd-MMM-yy"));
System.out.println(format);
DateTimeFormatter.ISO_DATE is shortcut for DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate inputDate = LocalDate.parse(input); would also work because ISO_DATE the default format
DateTimeFormatter doc (patterns)
The pattern is case sensitive:
MM is month, mm is Minute:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
For more Information read The javadoc of SimpleDateFormat
personally i used a pattern to do this,
LocalTime w = LocalTime.MIDNIGHT.plus(d);
s = DateTimeFormatter.ofPattern("HH:mm:ss").format(w);
and i advise you to use java 8 date type instead
I am using jQuery Datepicker that is giving the date like 07/05/2015 this format.I am using simpledateformat to format this date.But always the SDF is converting it to the todays date.How to solve this ??
System.out.println("Activity IS : IS With Date");
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd ");
Date startDate = ParamUtil.getDate(resourceRequest, "startDate", sdfOverTimeWithDate);
Date endDate = ParamUtil.getDate(resourceRequest, "endDate", sdfOverTimeWithDate);
int jobId= ParamUtil.getInteger(resourceRequest, "jobId");
System.out.println("jobId :"+jobId);
System.out.println("startDate :"+startDate);
System.out.println("endDate :"+endDate);
This statDate and endDate is giving todays's date only ,while the date i am pssing is in the format 07/05/2015.How to solve this ??somebody plaese help
You are passing wrong date format to SimpleDateFormat constructor. Try "dd/MM/yyyy" instead of "yyyy-MM-dd "
Is "07/05/2015" in July (US-Format) or May? Please clarify. If it is US-format (date expression starting with month number) then your solution is to use the pattern "MM/dd/yyyy" otherwise you should use the pattern "dd/MM/yyyy".
The pattern "yyyy-MM-dd " cannot be right due to two reasons:
a) It starts with a four-digit-year but your input begins with a two-digit-number.
b) It contains a trailing space.
Try to do it.
SimpleDateFormat sdfOverTimeWithDate = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdfOverTimeWithDate.parse("07/05/2015");
} catch (ParseException e) {
e.printStackTrace();
}
I have this weird format of date and time on my Oracle SQL Developer :
2015-4-14.1.39. 33. 870000000
I tried to format the given date to MM-dd-yyyy HH:mm:ss, but it gives me exception:
java.text.ParseException: Unparseable date: "2015-4-14.1.39. 33. 870000000"
The following is the code:
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
try {
String ts = "2015-4-14.1.39. 33. 870000000";
Date date = formatter.parse(ts);
String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(date);
System.out.println(S);
} catch (Exception e) {
e.printStackTrace();
}
The problem is that the given date string does not match the specified format. Try use the following format:
SimpleDateFormat df = new SimpleDateFormat("yyyy-M-dd.H.m. s. S");
String ts = "2015-4-14.1.39. 33. 870000000";
df.parse(ts);
Where
yyyy for year
M for month in year
dd for day in month
H for hour in date (0-23)
m for minute in hour
s for second in minute
S for millisecond
I have a string that contains a date, in the following format:
dd-mm-yyyy
with the month that is all lowercased. For example:
25-aug-2019
Now i tried to use SimpleDateFormat to convert my string to a Date, but i have the following exception:
java.text.ParseException: Unparseable date: "25-aug-2019"
at java.text.DateFormat.parse(Unknown Source)
at org.whoislibrary.servers.WhoisCom.parseResponse(WhoisCom.java:37)
at org.whoislibrary.WhoisAbstract.executeQuery(WhoisAbstract.java:44)
at org.whoislibrary.WhoisCommand.executeQuery(WhoisCommand.java:69)
at org.whoislibrary.WhoisMain.main(WhoisMain.java:10)
This is the code that i used:
DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
try {
Date expDate = df.parse(dateString).trim());
System.out.println(expDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
I think the problem is that MM refers to month name that start with a capital letter (Aug, Jul etc). There is an option, or a class like SimpleDateFormat that help me to convert that string into a Date. or it must be done Manually?
Well in your code you have:
new SimpleDateFormat("dd-MM-yyyy")
Which should be:
new SimpleDateFormat("dd-MMM-yyyy")
Since the month part in your string has 3 letters (MMM) and not 2 (MM)
use dd-MMM-yyyy
Your date "25-aug-2019" is in "dd-MMM-yyyy" format not "dd-MM-yyyy". So you get parse error. You should use "dd-MMM-yyyy" while creating SimpleDateFormat object.
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date expDate = df.parse("25-aug-2019");
System.out.println(expDate.toString());
} catch (ParseException e) {
e.printStackTrace();
}
Refer javaDocs java.text.SimpleDateFormat
I am developing an application and I am stuck in converting string like 01/01/2037 01:00:00 AM
to Date
I used
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa")
Date d = dateFormat.parse(dateString);
but I get an error, any help will be appreciated.
you are converting this 01/01/2037 01:00:00 AM
therefore use
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa")
(more info in [documentation])1
then
Date date = dateFormat.parse("01/01/2037 01:00:00 AM");
keep in mind you have to wrap a try-catch around the parse method.
The problem is that the format you declared is nothing like the String you are trying to parse:
your String uses / to separate day, month, year while in your formatter you use -
your string separates hours with a dot, while in the formatter you use :
you do not have milliseconds in your string while you declared them in the formatter.
The following code should work:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S aa");
try {
Date date = dateFormat.parse("01-01-2037 01.00.00.000 AM");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}