This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
Closed 5 years ago.
I was trying to convert date from yyyy-MM-dd format to yyyy-MM format, when I run the below code with the input "2012-10-22" it is giving me an output of 2012-JAN instead of 2012-OCT. any thoughts on where I am doing wrong?
public static String dateFormatter(String presentDate)
{
String formattedDate = "";
SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat finalFormat = new SimpleDateFormat("YYYY-MMM");
try {
Date currentFormat = tempFormat.parse(presentDate);
formattedDate = finalFormat.format(currentFormat);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formattedDate;
}
Change the first format to
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
as DD is the day in the year. 22 is definitely in January
Use this
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
Instead of
SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");
Related
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 5 years ago.
I was trying to convert date string 08-12-2017 to 2017-12-08(LocalDate). Here is what I tried-
String startDateString = "08-12-2017";
LocalDate date = LocalDate.parse(startDateString);
System.out.println(date);
Also tried using formatter, but getting same result, an DateTimeParseException.
How can I get an output like 2017-12-08, without getting an exception?
Try this (see update below)
try {
String startDateString = "08-12-2017";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.format(sdf.parse(startDateString)));
} catch (ParseException e) {
e.printStackTrace();
}
Update - Java 8
String startDateString = "08-12-2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.parse(startDateString, formatter).format(formatter2));
First you have to parse the string representation of your date-time into a Date object.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse("2011-11-29 12:34:25");
Then you format the Date object back into a String in your preferred format.
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String mydate = dateFormat.format(date);
This question already has answers here:
Java Unparseable Date Exception
(3 answers)
Closed 7 years ago.
Android comparing date with yyyy/MM/dd format only force closes when compared with yyyy-MM-dd format.
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault());
String d = "2016/12/29";
expiryDate = (Date) formatter.parse(d);
I hope this one help you.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date finalStartTime = format.parse(YOUR_OLD_DATE);
Date finalEndTime = format.parse(YOUR_NEW_DATE);
if (finalEndTime.after(finalStartTime)) {
//your code
}
} catch (ParseException e) {
}
If still not worked let me know...
Just Change "-" to "/" :
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd",
Locale.getDefault());
String d = "2016/12/29";
try {
expiryDate = (Date) formatter.parse(d);
} catch (ParseException e) {
//do something
}
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
String d = "2016/12/29";
Date expiryDate = formatter.parse(d);
This question already has answers here:
String to Date in Java
(4 answers)
Closed 7 years ago.
I want to convert the following String: "1:13 PM" to time format.
I am getting the following error: Unhandled exception:java.text.ParseException.
Here is my code:
String str = "1:13 PM";
DateFormat formatter = new SimpleDateFormat("hh:mm:ss a");
DateFormat date = formatter.parse(str);
Remove the ss, Its indicate second and you are not giving second in the String, So when the parser looking for the second value it cannot find the second giving you the exception.
DateFormat formatter = new SimpleDateFormat("hh:mm a");
And also change the next line to
try {
Date date = formatter.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
Answer is same as #Saif but formatter.parse(str) will not convert it to DateFormat object it will convert it in Date object.
String str = "1:13 PM";
DateFormat formatter = new SimpleDateFormat("hh:mm a");
try{
Date date = formatter.parse(str);
}catch(ParseException e){
e.printStackTrace();
}
This question already has answers here:
How to compare dates in Java? [duplicate]
(11 answers)
Closed 7 years ago.
I have a date coming in this format -
2015-4-10T11:20:56
I need to validate the date and make sure date it is not greater than current date. Meaning if today is April 10th, then it should not be April 11th or greater than that.
String abc = "2015-4-10T11:20:56";
if(abc is greater than todays date) {
// log an error
}
How can I do this?
UPDATE:-
I tried parssing like this but it didn't worked -
String abc = "2015-4-10T11:20:56";
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
try {
format.parse(abc);
} catch (ParseException e) {
e.printStackTrace();
}
You have to convert the string to a date object.
You can use a SimpleDateFormat.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d'T'HH:mm:ss");
Date date = sdf.parse(dateStr);
if (date.compareTo(new Date()) > 0) {
// log an error
}
This should work for you:
String abc = "2015-4-10T11:20:56";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
Date mydate = df.parse(abc);
Calendar c = Calendar.getInstance();
c.setTime(mydate);
Calendar today = Calendar.getInstance();
if (c.compareTo(today)>=0){
}
You can try like this using compareTo
Date date = null;
String str = "2015-4-10T11:20:56";
try {
DateFormat f = new SimpleDateFormat("yyyy-M-d'T'HH:mm:ss");
f.setLenient(false);
date = f.parse(str);
if (date.compareTo(new Date()) > 0) {
// your code
}
} catch (ParseException e) {
e.printStackTrace();
}
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