After converting string to date format month showing different [duplicate] - java

This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 4 years ago.
I am converting string to date format year and date showing correct but month showing different.
String s = "2018-08-29"
try
{
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD", Locale.ENGLISH);
Date parse = formatter.parse(s);
long time = parse.getTime();
}catch (ParseException e) {
e.printStackTrace();
}
output: Mon Jan 29 00:00:00 GMT+05:30 2018

The problem is in your format.
D stands for day in year, but you need to get day in month with d.
This should be like this:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Source
I hope it's helpful

Sorry I missed it at first. Your date should be simple letters "dd" not "DD"
DD is day in year
dd is date in month

Related

Wrong date conversion [duplicate]

This question already has answers here:
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 5 years ago.
I have a problem in date conversion in my android application.
I have date strings like 2017-11-11 11:52 which its date is equal to 2017-Nov-11 but it is parsed as 2017-01-11 in below code snippet:
DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm");
try {
Date date = df.parse("2017-11-11 11:52");
Log.v("DATE_TAG","Date Time:"+date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
The log output of above code is "Wed Jan 11 11:52:00 GMT+03:30 2017".
Is there anything wrong in my date format string?
You are using a wrong dateformat.
DD stands for the day of the year, not the day of the month. You have to use dd instead.
You can check the SimpleDateFormat documentation, where it is stated that DD can range from 1 to 365.
So your code should look like this:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date date = df.parse("2017-11-11 11:52");
Log.v("DATE_TAG","Date Time:"+date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
D is day in year e.g 189. Use d instead
Clearly noted from your output : Read Document
D is Day in year (1-365)
d is day in month (1-31)
Change this
DateFormat df = new SimpleDateFormat("yyyy-MM-DD HH:mm");
to
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
D is used for (Day in year), so in you case you need to use d (is day in month ). please use this Format "yyyy-MM-dd HH:mm"

Format a date to a specific format in java? [duplicate]

This question already has answers here:
Setting date into "dd"-"mm"-"yy" format from "dd"-"mm"-"yyyy" in java
(4 answers)
Closed 6 years ago.
I have a date in the format 11-NOV-13
When i convert the date i get the value Sat Nov 11 00:00:00 IST 13.
But i want the converted date to be in the format Sat Nov 11 00:00:00 IST 2013.
Though i have used the dateformat it is not taking it. Can anyone point me where i am wrong?
Below is the code i have been working on
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
Change the format to:
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
You need to make the year 2 numbers:
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
You can reference the docs here.
As you are not providing the full year you need
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
System.out.println(myDate);
Change to this format:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
String formattedDate = sdf.format(myDate);

How to convert 24 hrs time to 12 hours format in java using the date from the database [duplicate]

This question already has answers here:
Converting 24hour time to 12hour time?
(6 answers)
Closed 6 years ago.
I just want to convert a date from the database to 12 hours format which is originally in 24 hours format which is something like this 2015-06-11T28:28:57.000Z. In here this time format (28:25:57) seems to be the next day (ie. 2015-06-12T04:28:57).
I have tried with the following:
String date = "2015-06-11T28:28:57.000Z";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss a");
Date date = formatter.parse(date);
but I got an error like unparsable date.
Your date formatter string was incorrect, Try this:
String dateStr = "2015-06-11T28:28:57.000Z";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date date = sdfInput.parse(dateStr);
System.out.println("Date:"+date ); //Fri Jun 12 04:28:57 IST 2015
// to get 12 hour date formate date-string from provided date
SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS ");
String dateStr2 = sdfOutput.format(date);
System.out.println("dateStr2:" + dateStr2); //2015-06-12 04:28:57.000

Simple date format reading month incorrectly [duplicate]

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 7 years ago.
I am working with converting Date Strings to Epoch times then back again, but it seems like when I convert to Epoch the month is ignored when I parse the date String
String timeString = "15 Aug 2005 13:07:58.035";
Date dateTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("DD MMM yyyy hh:mm:ss", Locale.US);
try {
dateTime = sdf.parse(timeString);
} catch (ParseException e) {
e.printStackTrace();
}
long convert = dateTime.getTime();
System.out.println(str);
System.out.println(convert);
Date epochToString = new Date(convert);
System.out.println(epochToString.toString());
Here is the output
15 Aug 2005 13:07:58.035
1105812478000
Sat Jan 15 13:07:58 EST 2005
When I calculated the epoch time by hand the date was indeed Jan 15 2005, so for some reason the MMM value is being ignored in my format.
Edit:
I should also note that I changed the format token to MM and tried a numerical representation in the string instead, but had the same exact results.
This is because DD means day in year. You want dd for day in month. See SimpleDateFormat Javadoc
I suggest you use HH for 24 hour time.

Date and time in java not working [duplicate]

This question already has answers here:
DateFormat does not work?
(4 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 8 years ago.
I need to take date in the format date/month/year hours:minutes (time in 12 hour format) in java and I write the following code to save the date in datetoday. But it does not work for me. The output is shown in the following format Tue Nov 11 12:10:00 IST 2014. Can anyone help me regarding the issue? Thanks in advance.
Date datetoday;
void todaysdate() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("DD/MM/yyyy HH:mm");
String inputString2 = sdf.format(date);
try {
datetoday = sdf.parse(inputString2);
} catch (ParseException ex) {
System.out.println(ex.getMessage());
}
System.out.println(datetoday);
}
You can try this
new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss z");
Use hh instead of HH to get 12 hours format

Categories