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();
}
Related
This question already has answers here:
Java date format conversion - getting wrong month
(8 answers)
How to parse a date? [duplicate]
(5 answers)
Closed 5 years ago.
My method is :
public String changeCurrentDate(Integer variant){
String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
String currentDate = currentTime.substring(0, 10);
System.out.println("currentDate " +currentDate);
String date = null;
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
try{
Date date3 = df.parse(currentDate);
df.format(date3);
System.out.println("date3 " +date3);
Date previousDate = DateUtils.addDays(date3, variant);
date = previousDate.toString();
return date;
}catch (Exception e){
}
return date;
}
Note : currentTime variable always have the value like "18/12/2017"
I'm expecting result of date in dd/mm/yyyy format. but it always gives "Wed Jan 18 00:12:00 IST 2017" like this.
Run Time Results :
currentDate 18/12/2017
date3 Wed Jan 18 00:12:00 IST 2017
You should return the formatted date, not the toString() of the date. Try this:
Date previousDate = DateUtils.addDays(date3, variant);
return df.format(previousDate);
df.format simply returns a String representation of the Date with the format applied, therefore the line you have in your code has no effect.
Try changing you code to use the formatted output instead:
public String changeCurrentDate(Integer variant){
String currentTime = TestApp.getInstance().getDriver().findElement(By.id("common.HeaderComponent.mainLayout.serverTimeLabel")).getText();
String currentDate = currentTime.substring(0, 10);
System.out.println("currentDate " +currentDate);
DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
Date date3 = df.parse(currentDate);
System.out.println("date3 " + df.format(date3));
Date previousDate = DateUtils.addDays(date3, variant);
return previousDate.toString();
}
Also - its bad to catch Exception, so you should remove that.
We should always try to find out the more convenient way so that we can further use it. In that case, we can use below method:
public String dateString(String input) {
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
String formattedDate = "";
try {
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
formattedDate = formatter.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}
You can easily call this method like below:
Date date3 = df.parse(currentDate);
df.format(date3);
String input = date3.toString();
String requiredDate = dateString(input);
System.out.println("requiredDate: "+ requiredDate);
Which returns the output like below:
requiredDate: 18/12/2017
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:
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 can I increment a date by one day in Java?
(32 answers)
Closed 8 years ago.
How do I get the next date(2014/03/21) given the current date(2014/03/20) in Java?
Code:
public static String getNextDate(String curDate) {
String nextDate="";
try {
//here logic to get nextDate
} catch (Exception e) {
return nextDate;
}
return nextDate;
}
Use SimpleDateFormat to get a Date-object from your string representation, then use Calendar for arithmetics followed by SimpleDateformat to convert the Date-object back to a string representation. (And handle the Exceptions I didn't do)
public static String getNextDate(String curDate) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
final Date date = format.parse(curDate);
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, 1);
return format.format(calendar.getTime());
}
use java Calendar and you can use to do some date arithmetic such as adding day, months and years
public static String getNextDate(String curDate) {
String nextDate = "";
try {
Calendar today = Calendar.getInstance();
DateFormat format = new SimpleDateFormat("yyyy/MM/dd");
Date date = format.parse(curDate);
today.setTime(date);
today.add(Calendar.DAY_OF_YEAR, 1);
nextDate = format.format(today.getTime());
} catch (Exception e) {
return nextDate;
}
return nextDate;
}
You can use Joda Time.
public static String getNextDate(String curDate) {
String nextDate="";
try {
LocalDate date = LocalDate.now();
date = date.plusDays(1);
nextDate = date.toString();
} finally {
return nextDate;
}
}
If you want a more native Java way you could split the string using String.split("/") and then add to the date part. However doing this requires that the carry is handled and that you track leap years.