Android Open Source - BsuirSchedule Date Util






From Project

Back to project page BsuirSchedule.

License

The source code is released under:

Copyright 2012 Andrei Senchuk Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softwar...

If you think the Android project BsuirSchedule listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.taviscaron.bsuirschedule.core;
/*  w  ww . j  av a2s  . co  m*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
    /**
     * Calculate BSUIR work week from date
     * 
     * @param date
     *            date of the day to calculating its work week
     * @return work week (1 - 4)
     */
    public static int workWeekFromDate(Date date) {
        GregorianCalendar endCalendar = new GregorianCalendar();
        endCalendar.setTime(date);
        
        int year = endCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH);
        if (month < Calendar.SEPTEMBER) {
            year -= 1;
        }
        
        GregorianCalendar startCalendar = new GregorianCalendar(year, Calendar.SEPTEMBER, 1);
        startCalendar.get(Calendar.WEEK_OF_YEAR); // side effect: forced
                                                  // populating WEEK_OF_YEAR
                                                  // field
        startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        Date startDate = startCalendar.getTime();
        
        int days = (int) ((date.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
        int week = (int) ((days / 7) % 4 + 1);
        
        return week;
    }
    
    /**
     * Format passed date as dd.mm.yyyy
     * 
     * @param date
     *            data to format
     * @return formatted string
     */
    public static String simpleDateFormat(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
        String format = df.format(date);
        return format;
    }
    
    public static int weekDayOfDate(Date date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_WEEK) - Calendar.MONDAY;
        if (day < 0) {
            day = Calendar.SATURDAY + day;
        }
        return day;
    }
    
    public static Date today() {
        return new Date();
    }
    
    public static Date tomorrow() {
        Date today = today();
        Date tomorrow = new Date(today.getTime() + 24 * 60 * 60 * 1000);
        return tomorrow;
    }
}




Java Source Code List

net.taviscaron.bsuirschedule.activity.LessonsListActivity.java
net.taviscaron.bsuirschedule.activity.MainActivity.java
net.taviscaron.bsuirschedule.activity.ManageSchedulesActivity.java
net.taviscaron.bsuirschedule.activity.SettingsActivity.java
net.taviscaron.bsuirschedule.adapter.LessonsListAdapter.java
net.taviscaron.bsuirschedule.adapter.SchedulesListAdapter.java
net.taviscaron.bsuirschedule.core.BitUtil.java
net.taviscaron.bsuirschedule.core.BsuirScheduleApplication.java
net.taviscaron.bsuirschedule.core.Constants.java
net.taviscaron.bsuirschedule.core.DateUtil.java
net.taviscaron.bsuirschedule.loader.ScheduleLoader.java
net.taviscaron.bsuirschedule.model.Lesson.java
net.taviscaron.bsuirschedule.model.LessonsListModel.java
net.taviscaron.bsuirschedule.model.Schedule.java
net.taviscaron.bsuirschedule.storage.DBHelper.java