I need to compare whether a date is after another date. For example, today is 12 January 2020. First, I tried this:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date pDate = dateFormat.parse("12/01/2020");
Date currentDate = new Date();
if (currentDate.after(pDate)) {
Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
}
Although both date are the same, I got the toast "after". Then I tried this:
Date currentDate = new Date();
if (currentDate.after(currentDate)) {
Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "same day", Toast.LENGTH_LONG).show();
}
This time I got the toast "same day". Lastly I changed to date to 13/01/2020:
Date pDate = dateFormat.parse("13/01/2020");
Date currentDate = new Date();
if (currentDate.after(pDate)) {
Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "before", Toast.LENGTH_LONG).show();
}
And I got the toast "before". The method seems working, but why the first code returned "after" even both date are the same?
Actually your first scenario works as expected. The date that you compare with current date is really before of current date. Let me explain:
pDate contains Sun Jan 12 00:00:00 where time part is 00:00:00
currentDate contains Sun Jan 12 17:05:19 where time part is 17:05:19
So, your currentDate always after of your pDate.
To overcome this you have to compare date part only.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date pDate = dateFormat.parse("12/01/2020");
Date currentDate = dateFormat.parse(dateFormat.format(new Date()));
if (currentDate.after(pDate)) {
Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
}
you can look at date class source code here, it shows that they use the number of milliseconds for each dates. you are ignoring the hours, minutes and seconds values, and that's why you got the first result
Problem solved by converting the today time to 00:00:00
private Date getZeroTimeDate(Date date) {
Date res;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
res = calendar.getTime();
return res;
}
Then
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date pDate = dateFormat.parse("12/01/2020");
Date currentDate = new Date();
if (getZeroTimeDate(currentDate).after(pDate)) {
Toast.makeText(getApplicationContext(), "after", Toast.LENGTH_LONG).show();
}
Related
I want convert timestamp to date and set time in this day to 12.00.
How can I do this?
If I use this:
Date date = new Date(event.getActionDate()*1000);
I can't set hour or minutes to this date, becouse methods for this operations are deprecated.
Explanation:
I have timestamp -
1461924872 // Fri, 29 Apr 2016 10:14:32 GMT
I want change hour in this timestamp (10:14 to 00.00).
long timeInMillis = 1461877200000l;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
System.out.println("Date1:"+calendar.getTime().toString());
calendar.set(Calendar.HOUR_OF_DAY, 0); //For 12 AM use 0 and for 12 PM use 12.
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date = calendar.getTime();
System.out.println("Date2:"+date.toString());
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();//get your local time zone.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
sdf.setTimeZone(tz);//set time zone.
String localTime = sdf.format(new Date(time) * 1000));
Date date = new Date();
date = sdf.parse(localTime);
private String getDate(String timeStampStr){
try{
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date netDate = (new Date(Long.parseLong(timeStampStr)));
return sdf.format(netDate);
}
catch(Exception ex){
return dateInStr;
}
}
let me know if it is working?
I'm trying to get the date of tomorrow and insert it into a Date type variable, but for some reason I'm getting an empty string.
Here his the code:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String time_Date_str = cal.getTime().toString();
try
{
Date myDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(time_Date_str);
Toast.makeText(this, "time_Date_str", Toast.LENGTH_SHORT).show();
t.setDueDate(myDate);
t.setHasDate(true);
} catch(Exception e){myDate=null;}
Please let me know what I'm doing wrong.
You're not using your sdf variable to format the date... try this:
String time_Date_str = sdf.format(cal.getTime());
I am converting a timestamp into date and time and setting the result on a textview.
For example 1443884578 is Sat 3 October 2015 18:02
I would like to set the above date and time into an alarm manager.After research i found a code that uses a date time picker.
public void onDateSelectedButtonClick(View v) {
// Get the date from our datepicker
int day = picker.getDayOfMonth();
int month = picker.getMonth();
int year = picker.getYear();
// Create a new calendar set to the date chosen
// we set the time to midnight (i.e. the first minute of that day)
Calendar c = Calendar.getInstance();
c.set(year, month, day);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
// Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
scheduleClient.setAlarmForNotification(c);
// Notify the user what they just did
Toast.makeText(this, "Notification set for: " + day + "/" + (month + 1) + "/" + year, Toast.LENGTH_SHORT).show();
}
However its getting only the date and fires the alarm the minute the date occurs.
PROBLEM: I would like to get the date and time from my textview and skip this date time picker in the format i have. Is this possible?
String input = "Sat October 3 2015 18:02"; // Instead of String input = "Mon Feb 06 2015";
Calendar cal = Calendar.getInstance();
Date date = new Date();
// Changed the format to represent time of day
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss", Locale.ENGLISH);
try {
date = sdf.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(date);
//We haven't parsed the seconds from the original date so this will result
//in 18:02:00 - 10seconds.
//For a correct calculation, you could parse the seconds as well
//See SimpleDateFormat above, but you would have to provide the original date
//with seconds as well
cal.add(Calendar.SECOND, -10);
scheduleClient.setAlarmForNotification(cal);
I have a start date (day, month, year) and need the date say 4 weeks from that date. How can I calculate that? I know how to find the difference between two dates using Calendar so I assume I'm not too far from the answer... Thank you!!!
edit:
This is the final code I wound up using. It returns a String whose value is a date span formatted "MM/dd/YYYY - MM/dd/YYYY"
#SuppressLint("SimpleDateFormat")
private String getSessionDate(int position) {
MySession ms = mSessionList.get(position);
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.set(ms.getStartYear(), ms.getStartMonth(), ms.getStartDay());
Date startDate = calendar.getTime();
String durationString = ms.getDurationString(); // Format is "## weeks"
int i = 0;
while (Character.isDigit(durationString.charAt(i))) {
i++;
}
int weeks = Integer.parseInt(durationString.substring(0, i));
calendar.add(Calendar.WEEK_OF_YEAR, weeks);
return (format.format(startDate) + " - " + format.format(calendar.getTime()));
}
You can use Calender instance for that.
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentdate);
calendar.add(Calendar.DAY_OF_YEAR, no_of_days)
Date newDate = calendar.getTime();
You can calculate the date by adding or subtracting the no of days
Example :
Get date after 1 week
calendar.add(Calendar.DAY_OF_YEAR, 7);
Get date before 1 week
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date date=null;
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = originalFormat.parse(strDate); // strDate is your date from which you want to get date after 4 weeks
} catch (Exception e) {
e.printStackTrace();
}
long timeFor4week=4*7*24 * 60 * 60 * 1000; /// here 24*60*60*1000 =24 hours i.e 1 day
long timeAfter4week=date.getTime()+timeFor4week;
String finalDateString=originalFormat.format(new Date(timeAfter4week));
So you can get day after 4 weeks.
I am developing an application in that when time is 11:26 it is showing 11:07. I used Calendar instance to do.
Calendar currentDate=Calendar.getInstance();
SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
datenow=ddMMyyyy.format(currentDate.getTime());
if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MONTH)+":PM";
}else{
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MONTH)+":AM";
}
new MyToast(this, "Date = "+datenow+" time = "+timenow);
The out put is wrong what to do?
you are displaying month in place of minute. Change your code to my code
Calendar currentDate=Calendar.getInstance();
SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
String datenow = ddMMyyyy.format(currentDate.getTime());
String timenow;
if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":PM";
}else{
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":AM";
}
Toast.makeText(MainActivity.this, "Date = "+datenow+" time = "+timenow,Toast.LENGTH_SHORT).show();
11:07 is displaying because 07 ==> month number in a year. month will be calculated from 0 -> 11
You have mistaken see here Calendar.HOUR + : + Calendar.MONTH. It should be as follows,
Calendar currentDate=Calendar.getInstance();
SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
datenow=ddMMyyyy.format(currentDate.getTime());
if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":PM";
}else{
timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":AM";
}
new MyToast(this, "Date = "+datenow+" time = "+timenow);
Instead of MINUTE you were accessing MONTH object of Calendar Class.