Wrong date conversion [duplicate] - java

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"

Related

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

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

How to convert est datetime String into GMT datetime in java [duplicate]

This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I will be reciving the Input in this format 201201 , which is YYYYMM format .
Now i want to return the value 201201 as it is , but it it should be in a java.util.Date format
I am confused
String strDate = "201201";
SimpleDateFormat sdFormat = new SimpleDateFormat("YYYYMM");
Now i am not able to return in the java.util.Date format with the value as 201201
I ahve edited the question it must be in YYYYMM format .
I tried this way
public class StringToDate {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyymm");
try {
Date today = df.parse("201201");
System.out.println(df.format(today));
//System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
EDIT: mm is minute MM is month
Try : DateFormat df = new SimpleDateFormat("yyyyMM");
Then use the parse method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29
The only thing I see off is your SimpleDateFormat declaration. It should be
DateFormat df = new SimpleDateFormat("yyyyMM");
Then, System.out.println(df.format(today)); will return today as "201201"
You'll need to use SimpleDateFormat to get any java.util.date into the desired format you want.
A java.util.date without formatting outputs like "Sun Jan 01 00:00:00 EST 2012" as a String.
A java.util.date object can't be set natively with a format of "yyyyMM".
SimpleDateFormat df = new SimpleDateFormat("yyyymm"); should be SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
MM = Month in year
mm = Minute in hour

Java how to make date from string [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 8 years ago.
how can i make Date data type from String in java?
input: String "2014/01/01 18:02:02" => output: Date 2014/01/01 18:02:02
You could use a DateFormat to parse and format (they're reciprocal functions) the String. Something like,
String str = "2014/01/01 18:02:02";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
Date d = df.parse(str);
System.out.println(df.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
Output is (as requested)
2014/01/01 18:02:02
please use SimpleDateFormat to parse String To Date.
In there you can find suitable DateFormat to convert this String to Date.
you can try this
String dateString = "2014/01/01 18:02:02";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime StringAsDate = LocalDateTime.parse(dateString, formatter);
I would recommend using Time(java.time) API instead of java.util for your Date & Time needs as it is new and exclusively added to java for Date & Time operations.
You can use the following code snippet -
String dateString = "10-11-2014";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(dateString);
You can try this too.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String dateInString = "2014/01/01 18:02:02";
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
It is a working and it is also suitable to you as it is not complicated. you will get this output:
Wed Jan 01 18:02:02 IST 2014
2014/01/01 18:02:02
For your date following code should work :-
String myString = "2014/01/01 18:02:02";
DateFormat myDateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date myDate = myDateFormat.parse(myString);
For more details, see the documentation for SimpleDateFormat. Just make sure you use capital "MM" for month and small "mm" for minute.

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

Java : Return a String in the java.util.Date Format [duplicate]

This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
I will be reciving the Input in this format 201201 , which is YYYYMM format .
Now i want to return the value 201201 as it is , but it it should be in a java.util.Date format
I am confused
String strDate = "201201";
SimpleDateFormat sdFormat = new SimpleDateFormat("YYYYMM");
Now i am not able to return in the java.util.Date format with the value as 201201
I ahve edited the question it must be in YYYYMM format .
I tried this way
public class StringToDate {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("yyyymm");
try {
Date today = df.parse("201201");
System.out.println(df.format(today));
//System.out.println("Today = " + df.format(today));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
EDIT: mm is minute MM is month
Try : DateFormat df = new SimpleDateFormat("yyyyMM");
Then use the parse method:
http://docs.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29
The only thing I see off is your SimpleDateFormat declaration. It should be
DateFormat df = new SimpleDateFormat("yyyyMM");
Then, System.out.println(df.format(today)); will return today as "201201"
You'll need to use SimpleDateFormat to get any java.util.date into the desired format you want.
A java.util.date without formatting outputs like "Sun Jan 01 00:00:00 EST 2012" as a String.
A java.util.date object can't be set natively with a format of "yyyyMM".
SimpleDateFormat df = new SimpleDateFormat("yyyymm"); should be SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
MM = Month in year
mm = Minute in hour

Categories