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();
}
Related
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
Hello Guys This problem is driving me crazy right now. I have a string in my database called dueDate with value Jan 18, 2018. I retrieved it and save it in a string in my recyclerAdapter with String dueDate = transactionTasks.get(position).get_transactiontaskpaydet().toString();
Now i wanna compare it with the current date and if the due date is after my current date it should display the dueDate with red color.
bellow are my coded.
String dueDate = transactionTasks.get(position).get_transactiontaskpaydet().toString();
Date cdate = new Date();
Date ddate = new Date(dueDate);
if(cdate.after(ddate)){
holder.date.setTextColor(Color.RED);
}
This codes work perfectly but the problem is Date(dueDate); is deprecated. And when i use another method that uses a try and catch, i don't get any result.
Below are the codes
SimpleDateFormat format = new SimpleDateFormat("E, MMM dd yyyy");
Date curDate = new Date();
try {
Date datedue = format.parse(dueDate);
if(curDate.after(datedue)) {
holder.date.setTextColor(Color.RED);
}
} catch (ParseException e) {
e.printStackTrace();
}
And if I try creating the Date outside the try block by using Date duedate = null; I get an error because its not getting the values inside the try block. this is driving me crazy because I shouldn't use deprecated code even if it works perfectly.
All the answers I found didn't work for me. I just need to be able to convert a string to a date so I can compare it with the current date. Thanks Guys
I think the issue is with your SimpleDateFormat. If your String really is "Jan 18, 2018" then you need your SimpleDateFormat to match that like this:
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
I made a quick method to test this and it worked fine:
public static void main(String args[]) {
String dueDate = "Jan 18, 2018";
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
Date curDate = new Date();
try {
Date datedue = format.parse(dueDate);
if(curDate.after(datedue)) {
holder.date.setTextColor(Color.RED);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I hope this helps!
This question already has answers here:
Java: Convert String to TimeStamp
(11 answers)
Closed 5 years ago.
I want to convert a string to Timestamp. Here is the sample code.
I want to parse the string to Timestamp..
I have tried to convert it like
String date format is "18/01/11";
String date = "25/07/2017";
Timestamp ts =Timestamp.valueOf(date);
DateUtil.convertDate(ts.toString());
convertDate:-
SimpleDateFormat inputFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
Date d = null;
try {
d = inputFormat.parse(input);
} catch (ParseException e) {
System.out.println("Date Format Not Supported");
e.printStackTrace();
}
SimpleDateFormat outputFormat = new SimpleDateFormat("dd/MM/yyyy");
return outputFormat.format(d).toString();
if I write like that i am getting the following error
Date Format Not Supported
Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
I think you are using just opposite formats if your input format is dd/MM/yyyy:
//date format of input
SimpleDateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
try {
//convert string to date
d = inputFormat.parse(input);
} catch (ParseException e) {
System.out.println("Date Format Not Supported");
e.printStackTrace();
}
//output date format
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//convert date to timestamp
return outputFormat.format(d).toString();
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");
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:
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();
}