This question already has answers here:
Comparing date strings in Java [duplicate]
(6 answers)
Closed 8 years ago.
I am getting date and time which is stored in a String.Below is the date and time which is stored in string.
String userDateTime = "26-Aug-2014 09.00.00 AM";
I have to compare current date and time with the one which is stored in String and if date or time is past date or time or equals to current date and time, it has to perform some logic. Please suggest how can i compare current date and time which is stored in String with the system date and time.
I can use java.util.Date but not sure how can i compare with string format.Please suggest.
First convert the string to date using below way:
SimpleDateFormat ss = new SimpleDateFormat("dd-MMM-yyyy HH.mm.ss a");
String dateInString = "26-Aug-2014 09.00.00 AM";
try {
Date date = ss.parse(dateInString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
hen you can compare the dates
You need to parse it first, then compare as a Date (or Joda DateTime) - not as String. There is no way you can achieve that with comparing Strings unless you implement some complicated logic using regex.
You can do something like this:
String string = "26-Aug-2014 09.00.00 AM";
Date date = new SimpleDateFormat("d-MMMM-yyyy", Locale.ENGLISH).parse(string);
See this answer from BalusC:
Java string to date conversion
Then you can compare them with date.compareTo(otherDate)
Related
This question already has answers here:
How do I check if a date is within a certain range?
(17 answers)
Closed 4 years ago.
I get three dates: From date, To date and current date.
I want to find whether current date is in between From and To dates. If current date is in between these two dates then I want to create two new From and To dates.
Example:
From Date = 15 march
To Date = 25 march
current date = 21 march
Expected result should be:
From Date= 15 march, To Date=21 march
From date= 21 march, To Date=25 march
To implement this logic I want to check my current date status whether it's in middle of date range or it's before or after.
You said in the comments that your inputs are "in date format", but that's a very vague description, because, technically speaking, Date objects don't have a format.
If your inputs are instances of java.util.Date, then just use the methods before, after and equals to compare the dates.
If you're using Java 8, the java.time API is a much better choice. You can either use a java.time.LocalDate or even a java.time.MonthDay, if you don't care about the year.
Both classes have comparison methods: isAfter, isBefore and equals, and a now() method to get the current date.
With those, you have all the tools to implement your logic.
Try something like this :
ArrayList<Date> dates = new ArrayList<>();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateString1 = "2017-02-15";
String dateString2 = "2015-03-12";
String dateString3 = "2016-07-19";
Date date1 = null,date2=null,date3=null;
try {
date1 = sdf.parse(dateString1);
date2 = sdf.parse(dateString2);
date3 = sdf.parse(dateString3);
} catch (ParseException e) {
e.printStackTrace();
}
dates.add(date1);
dates.add(date2);
dates.add(date3);
Collections.sort(dates, new Comparator<Date>() {
public int compare(Date d1, Date d2) {
return d1.compareTo(d2);
}
});
This question already has answers here:
How do I convert the date from one format to another date object in another format without using any deprecated classes?
(10 answers)
Closed 5 years ago.
I am doing some junit test. I am getting the date as response in the form of date as below :
2017-08-14 00:00:00.0 +0:00
The data which is present in oracle DB is
14-AUG-17 12.00.00.000000000 AM +00:00
I want to use an assert like this but it is failing. can anyone help to make sure that both expected and actual matches.
Assert.assertEquals("14-08-2017", 2017-08-14 00:00:00.0 +0:00);
You can create two Date objects and for the assertion.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
try {
Date parsedDate1 = formatter.parse("14-08-2017");
System.out.println(parsedDate1);
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate2 = formatter2.parse("2017-08-1 00:00:00.0 +0:00");
Assert.assertEquals(parsedDate1, parsedDate2);
} catch (ParseException e1) {
}
You can use SimpleDateFormat to make string from date.
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Assert.assertEquals("14-08-2017", sdf.format(<your date instance>));
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
how to convert this string "2016-10-08T01:00:00-07:00" to date object in Java?
I want to know what is string format to use with SimpleDateFormat.
I have try
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = format.parse("2016-10-08T01:00:00-07:00");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Change your format from Z to X will work . Detail is here
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = format.parse("2016-10-08T01:00:00-07:00");
should be
Date date = format.parse("2016-10-08T01:00:00-0700");
The time zone should not have a colon delimiting hours and minutes.
You can try SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Best solution to refer # Change date format in a Java string
This question already has answers here:
How to add 10 minutes to my (String) time?
(8 answers)
How can I read and parse a date and time in Android?
(2 answers)
Closed 7 years ago.
I am calling an API and as per the guidelines calling it every time is inefficient as the data does not change that regularly. They recommend calling it once and then not polling until 10 minutes has passed.
I am calling this from an Android app and so I want to store the current date plus 10 minutes. I do this like so:
Date forecastRefreshDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(forecastRefreshDate);
cal.add(Calendar.MINUTE, 10);
editor.putString("ForecastRefreshDate", forecastRefreshDate.toString());
editor.apply();
So now when this code is run again it needs to check if the current time (new Date()) is > the value saved in the cache/app file.
How do I create a Date variable equal to the value stored in the cache as a String?
Convert date to epoch time, which is a number of milliseconds stored as a long. Store long as string and parse back into long when needed.
long epoch = date.getTime(); //where date is your Date
String yourString = Long.toString(epoch); //to string for storage
long time = Long.valueOf(yourString).longValue(); ; //back to epoch
Date originaldate = new Date(Long.parseLong(time)); //back to date
When dealing with time, which is needed only internally to measure time periods,
It is much better to store timestamps as long value (millis sicne 1.1.1970 UTC).
Use long timestamp = System.currentTimeMillis()
If a String storage is neccesary simply convert the Long to a String.
String timeStampStr = String.parseLong(timeStamp);
Do not use Date.toString for other things than for logging or debugging.
The toString() representation may be different in other countries.
If a human readable timestamp string is needed you have to specify a Specific DateFormat, see also DateFormatter.
Parsing a long it the most efficent way, however there may be cases that you have already formatted a date to some other format and want to parse that. Then you should use this
private static final String format = "yyyy-MM-dd HH:mm:ss";
private static final SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH);
private Calendar dateTime = Calendar.getInstance();
public void setDateTime(String dateTimeString) {
try {
dateTime.setTime(formatter.parse(dateTimeString));
} catch (ParseException e) {
// Dummy handled exception
dateTime.setTimeInMillis(0);
}
}
Parse a long containing milliseconds since epoch
Date date = new Date(Long.parseLong(timeInMillisecondsString));
Since you're using a plain toString() it should be coverable using a SimpleDateFormat.
Try this:
DateFormat dateFormat = new SimpleDateFormat();
Date myDate = dateFormat.parse(myDateString);
This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 8 years ago.
i am making a university project assignment and i am trying to calculate the days between two dates, but so far i did not achieved anything.
The problem is:
I receive two strings in this format "NNNN#AAAA-MM-DD" and i need to check if the dates are alright and all of that. I already made the methods to do it and they are working. The problem is that i cant figure it out how to see the days between without the use of calendar method or date or any type of those ones.
Thanks ;)
you can use this method for convert the string in date:
public Date convertStringToDate(String dateString)
{
Date date = null;
Date formatteddate = null;
DateFormat df = new SimpleDateFormat("NNNN#yyyy-MM-dd");
try{
date = df.parse(dateString);
formatteddate = df.format(date);
}
catch ( Exception ex ){
return null;
}
return formatteddate;
}