how to check if system date is between two times in android? - android

I have a start date = "13:45"
and an end date = "14:35"
Now i would like to check if the system time is between these two times, for example "14:00"
I have tried the methods before and after from the Date class but it did not worked properly
Any Suggestions ?

SimpleDateFormat format = new SimpleDateFormat("HHmm",
Locale.getDefault());
String now = format.format(new Date());
String start = "13:45".replace(":", "");
String end = "14:35".replace(":", "");
boolean isBetween = Integer.valueOf(now) > Integer.valueOf(start)
&& Integer.valueOf(now) < Integer.valueOf(end);
System.out.println(isBetween);

Maybe you could do something like this:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, 13);
start.set(Calendar.MINUTE, 45);
Calendar current = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, 14);
end.set(Calendar.MINUTE, 35);
if(current.after(start) && current.before(end)){
//we are between
}

Related

How to fetch recent seven days from specific date

If the date is 2017-03-30 that i want to fetch the date from 2017-03-23 to 2017-03-30
I try to use this code let my String change to Date format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dateParse = sdf.parse("2017-03-30");
then i'm stuck , cause i take the reference is get the current time like this
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -7);
//may be my dateParse should put here , but i don't know how to do
Date monday = c.getTime();//it get the current time
String preMonday = sdf.format(monday);
Is any one can teach me how to fetch these seven days ? Thanks in advance.
You can use the code below
SimpleDateFormatdateFormat = new SimpleDateFormat("dd MMM yyyy");
Calendar c = Calendar.getInstance();
String date = dateFormat.format(c.getTime());
c.add(Calendar.DATE, 7);
String date1 = dateFormat.format(c.getTime());
Parse the date:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = sdf.parse("2017-03-30");
First Solution 1) And then either figure out how many milliseconds you need to subtract:
Date newDate = new Date(myDate.getTime() - 604800000L); // 7 * 24 * 60 * 60 * 1000
Second Solution 2) Or use the API provided by the java.util.Calendar class:
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -7);
Date newDate = calendar.getTime();
Then, if you need to, convert it back to a String:
String date = dateFormat.format(newDate);
This answer is from here
EDIT:
If you need output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23 then try below code
for(int i = 1; i <= 7; i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -i);
Date newDate = calendar.getTime();
String date = dateFormat.format(newDate);
//here in date you can get all date from and output as 2017-03-29 2017-03-28 2017-03-27 ...... 2017-03-23
}
Hope you need this

Get yesterday from simpledataformat

It's my code for getting today to long.
Can I get yesterday by using this code?
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
long date_t = Long.valueOf(date_0.format(date_1));
EDIT>
I solve with this
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
long date_t = Long.valueOf(date_0.format(date_1));
long date_2 = date_1.getTime();
Date yesterday = new Date(date_2 -= 86400000);
long date_y = Long.valueOf(date_0.format(yesterday));
You could take the current date, and subtract 86400000 milliseconds (equivalent to one day).
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
long date_t = date_1.getTime();
date_t -= 86400000;
Date yesterday = new Date(date_t);
System.out.println("Yesterday's Date: " + date_0.format(yesterday));
I'm not sure but this should work:
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Date date_1 = new Date();
date_1.setDate(date_1.getDay()-1);
long date_t = Long.valueOf(date_0.format(date_1));
SimpleDateFormat date_0 = new SimpleDateFormat("yyMMdd");
Calendar calendar = Calendar.getInstance(); // this is default system date
calendar.add(Calendar.DAY_OF_MONTH, -1); // minus date to previous day
long date_t = Long.valueOf(date_0.format(calendar.getTime())); // convert into long
System.out.println(date_0.format(calendar.getTime())); // system print 151110
You can use Joda-Time. The design allows for multiple calendar systems, while still providing a simple API.
private DateTime getPreviousDateAndTime(int previousCount){
DateTimeFormat format = DateTimeFormat.forPattern("yyyy-MM-dd 00:00:00.000000000");
DateTime now = new DateTime();
DateTime expectedDate = now.minusDays(previousCount);
return expectedDate;
}
Add the following dependency to build.gradle:
dependencies {
compile 'net.danlew:android.joda:2.9.0'
}
Simple..
private Calendar calendar;
public void getPreviousDay() {
calendar.add(Calendar.DATE, -1);
}

set text in a textview based on time android

I want to display a message in a textview, the code that i use right now is
mText = (TextView) findViewById(R.id.textView19);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String str = sdf.format(new Date());
String[] hr=str.split(":");
int hr1=Integer.parseInt(hr[0]);
if(hr1<12)
{
mText.setText("it's morning!");
}else if(hr1>12&& hr1<17)
{
mText.setText("it's afternoon!");
}else if(hr1>17&& hr1<20)
{
mText.setText("it's evening!");
}
But that code doesnt work at all it doesnt type anything in the textview.
How do i fix this?
You are creating a null date, you need to get the current time by doing:
Calendar c = Calendar.getInstance(); // Get current time
int hr1 = c.get(Calendar.HOUR_OF_DAY); // Gets the current hour of the day from the calendar created ( from 1 to 24 )
Check the Calendar documentation here.

How to compare two Date and Time and get Day,Month,hour,min in Android?

I Have this type of Date in String Format 01-18-2013 06:43:35 Now, i want to compare this Date with Current Date and Time and get Day, Month, Hour, Min, .. I Searched this link but didn't get any Solution...Please share some solution..Thank you..
This might help,
http://developer.android.com/reference/java/text/DateFormat.html
You can parse the Date from string using
DateFormat df = DateFormat.getDateInstance();
myDate = df.parse(myString);
If I get your question, you would like to Compare a Date object with the current date..
Let's say that 'date' is the Date object you want to compare with the current date:
Why don't you just do something like date.after(new Date()) or date.before(new Date()) as suggested form the android doc?
You can get UTC with
new SimpleDateFormat("MM-dd-yyyy HH:mm:ss").parse("01-18-2013 06:43:35").getTime();
Then compare the result with
System.currentTimeMillis();
this may helps you to calculate to diffrent date time in millisecond
try{
Calendar calender = Calendar.getInstance();
Calendar calDb = Calendar.getInstance();
Calendar matchd = Calendar.getInstance();
mYear = calender.get(Calendar.YEAR);
mMonth = calender.get(Calendar.MONTH);
mDay = calender.get(Calendar.DAY_OF_MONTH);
mendYear = calDb.get(Calendar.YEAR);
mendMonth = calDb.get(Calendar.MONTH);
mendDay = calDb.get(Calendar.DAY_OF_MONTH);
// Here you can change day values
calDb.set(Calendar.DAY_OF_MONTH, mDay-1);
strbeforedate = mDateFormat.format(calDb.getTime());
curentdate = mDateFormat.format(calender.getTime());
calDb.setTime(mDateFormat.parse(strbeforedate));
calender.setTime(mDateFormat.parse(curentdate));
String mydate = "2013.03.14 03:11";
String mdatetime = "";
deletepath = new ArrayList<String>();
for(int i=0;i<result.length;i++){
try{
// here your matching goes and pass date here
matchd.setTime(mDateFormat.parse(mdatetime));
long diff = calDb.getTimeInMillis() - calender.getTimeInMillis();
long matchdiff = matchd.getTimeInMillis() - calender.getTimeInMillis();
if(diff < matchdiff){
// do your work here
}else{
// do your else work here
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}

Android: Calendar not returning values?

I'm trying to run this little code to return a eight-digit integer to be used in a for-loop later on as a search function. Problem is that it doesn't return any values for searchDateToday. Am I missing something?
final Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
DateToday = formatter.format(cal.getTime()); // YYYYMMDD form -
// Example: 20111010
// = October 10,
// 2011
int searchDateToday = Integer.parseInt(DateToday);
You forgot to use the Date object:
final Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
DateToday = formatter.format(new Date(cal.getTime()));
// YYYYMMDD form -// Example: 20111010 // = October 10, // 2011
int searchDateToday = Integer.parseInt(DateToday);
use this for getting today date
String currentDate = DateFormat.getDateInstance().format(new Date());
tv_date.setText(currentDate); // tv_date is TextView

Categories

Resources