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);
Related
This question already has answers here:
12:xx shown as 00:xx in SimpleDateFormat.format("hh:mm:ss")
(1 answer)
Difference between java HH:mm and hh:mm on SimpleDateFormat
(5 answers)
Closed 1 year ago.
public static String getTime(String time) {
String ampmTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss", Locale.getDefault());
Date dt;
try {
dt = sdf.parse(time);
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a", Locale.getDefault());
if (dt != null) {
ampmTime = sdfs.format(dt);
}
} catch (ParseException e) {
e.printStackTrace();
}
return ampmTime;
}
In parameter string i'm sending 12:00:00 format so please i want this as 12:00pm instead of 12:00am . And others time is showing correctly like 13:30:00 to 1:30pm . So please help me??
As stated here, When using hh you are telling the formatter the input time is an am/pm time. Use HH for 24hrs input.
Change the format to:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
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 string to date conversion
(17 answers)
Closed 7 years ago.
I have a time string like 4:52 pm. I want to convert it in milliseconds for comparison with current time of system. How is it possible in android ?
long millis = 0;
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
try {
Date d = dateFormat.parse("4:52 PM");
millis = d.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
String someTime = "4:52 pm";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
Date date = sdf.parse(someTime);
System.out.println(date.getTime());
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();
}