Outreachy- Week 6 & 7 Progress

Working with Date, Calendar, SimpleDateFormat   in Android.

As I mentioned In my last blog, I would talk about how I used Calendar and Date classes for the user to designate silent mode by setting time constraints and weekdays, in Lumicall.

Date class to used to interpret dates as year, month, day, hour, minute, and second values.

I had to compare whether the current time falls between Start Time and End Time specified by the user. So that, silent mode can be enabled within that time frame.

I used Calendar Class to get the current hour in 24-Hour format and minute.

 Calendar now = Calendar.getInstance();

 int hour = now.get(Calendar.HOUR_OF_DAY);  

 int minute = now.get(Calendar.MINUTE);

Now, Since the user enters the Time in EditText Widgets, the values were retrieved as strings.
String hhStart, mmStart, hhEnd, mmEnd store these values from Edit text widgets.

To interpret these strings as a representation of a date and time, we need to parse it

A “Date” class object’s format looks like-

unnamed
Since, I was only interested in fetching the HH:MM values, i.e the fourth field in the format,

To set and compare only the HH:MM values, Android provides lovely, SimpleDateFormat class to access the particular value we want in the Date object.
To access the year, use letter Y
To access the time zone, use letter z
To access the Hour and minute, we use letter H and m.

SimpleDateFormat simpleDateFormat= new SimpleDateFormat(“HH:mm”, Locale.ENGLISH);

Date currentTime = parseDate(hour + ":" + minute)

Date timeCompareOne = parseDate(hhStart +”:”+mmStart);  

Date timeCompareTwo = parseDate(hhEnd +”:”+mmEnd);


Rest everything in the date object are values set by default. Eg, Year 1970. Which we din’t set / access , hence did not change.

To check if the start time is before the current time. And the endtime is after the current time, 

if(timeCompareOne.before(currentTime) && timeCompareTwo.after(currentTime))

{

Switch on the silent mode;

}

Added a try catch block to handle the exception which will arise if the SimpleDateFormat.parse method is unable to parse the given Java String.

public Date parseDate(String date)  
{ 
try 
{  
return inputParser.parse(date);
} 
catch (java.text.ParseException e)  
{  
return new Date(0);  
}  
}

Comparing Time? Done!
Now to check whether the selected weekday in the checkboxes matches the current week day,

Calendar calendar = Calendar.getInstance(); 

int day = calendar.get(Calendar.DAY_OF_WEEK); 

If it’s sunday, value returned by calendar.get(Calendar.DAY_OF_WEEK) is 1, if monday, 2 and so on..
Weekdays compared too! 🙂

Thanks for reading,
U

Advertisements

One thought on “Outreachy- Week 6 & 7 Progress

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: