1

So I've been going over some of the date classes in java and in popular libraries, and they all seem overly bloated, or are too bugged/complicated for me to use. The closest I've found is Java's Calendar class. But it's still too complicated. So I'm looking for a super simple DateTime class.

I need this for an Android app I'm currently working on. In this app I read a week schedule from a website to display in my app. So all I need is:

  • years
  • months
  • perhaps also weeks, but I can read that from the schedule as well
  • days
  • hours
  • minutes

Second and millisecond precision are not necessary. I also don't need timezones, since the app is only for my current college. The only support I need for special cases is:

  • days per month (28, 30, 31)
  • leap years

I know already that someone will probably suggest Joda-Time, but it's bloated, and I've read multiple reports about how slow it is on Android.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Simon Verbeke
  • 2,905
  • 8
  • 36
  • 55
  • 1
    I use JodaTime and love it. It is only slow on Android because of an issue with TimeZone providers that can be easily fixed: http://stackoverflow.com/questions/5059663/android-java-joda-date-is-slow – Ricardo Nov 25 '13 at 16:41
  • @EmbattledSwag for example the fact that months start at 0. Also that I have to set individual fields by using the field's index, instead of a simple setDay() – Simon Verbeke Nov 25 '13 at 16:46
  • Date and Time classes are not simple because dates and times are not simple. – Basil Bourque Nov 26 '13 at 06:04

6 Answers6

1

you can use this:

Date todayDate = new Date();
todayDate.getDay();
todayDate.getHours();
todayDate.getMinutes();
todayDate.getMonth();
todayDate.getTime();

EDIT:

better way to do is to use Calendar:

Calendar cal = Calendar.getInstance(); 

int millisecond = cal.get(Calendar.MILLISECOND);
int second = cal.get(Calendar.SECOND);
int minute = cal.get(Calendar.MINUTE);
    //12 hour format
int hour = cal.get(Calendar.HOUR);
    //24 hour format
int hourofday = cal.get(Calendar.HOUR_OF_DAY);

Same goes for the date, as follows:

Calendar cal = Calendar.getInstance(); 

int dayofyear = cal.get(Calendar.DAY_OF_YEAR);
int year = cal.get(Calendar.YEAR);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
int dayofmonth = cal.get(Calendar.DAY_OF_MONTH);
lugonja
  • 303
  • 4
  • 15
0

You do need time zones because you probably do or did have daylight savings time in your country. I highly recommend reading http://www.odi.ch/prog/design/datetime.php to get a basic understanding of how this will affect you.

It's just better to properly implement this 'cause it will bite you in the ass later if you don't.

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73
  • Perhaps you're right, but I doubt it. I read all dates and times from another website, which presumably has this sorted already. Besides that, my week schedule will only use weekdays, and daylight savings time switches during the weekends. – Simon Verbeke Nov 25 '13 at 16:51
  • So how are you going to calculate the amount of hours between date 1 and date 2 if you don't know if there is going to be a DST shift (even in a weekend) between the dates? That's just one of the common examples. – Sebastiaan van den Broek Nov 25 '13 at 16:52
  • I don't need calculations. I read all dates and times from a website. – Simon Verbeke Nov 25 '13 at 16:56
  • 1
    Well in that case you don't need special support for amount of days in a month and stuff either right? You could just store the various components in a simple class. You could even just store the whole thing as a String. But I hope you'll never have to add some new features that do something complex. – Sebastiaan van den Broek Nov 25 '13 at 16:58
  • Good point, I actually could make a class myself. There really isn't much to it if I just store dates. – Simon Verbeke Nov 25 '13 at 17:00
0

Try this:

Calendar calendar = Calendar.getInstance();
    int date = calendar.get(Calendar.DATE);
    int month = calendar.get(Calendar.MONTH);
    int year = calendar.get(Calendar.YEAR);
0

Date is deprecated...generally. Use Calendar or as suggested Joda Time

Calendar calendar = Calendar.getInstance();
int Year = calendar.get(Calendar.YEAR);
int Month = calendar.get(Calendar.MONTH + 1);
int Day = calendar.get(Calendar.DAY_OF_MONTH);
EdGs
  • 356
  • 6
  • 18
0

As suggested by Sebastiaan van den Broek, I decided to make my own simple class for storing dates and times. If anyone should need it, I included the code.

Be aware that there's only support for storing dates and times generated by a system that accounts for leap years, time zones, etc. There are probably also a couple of functions missing for simple storage, but this class is - at the moment - sufficient for my case.

/**
 * A simple class for storing dates and times.
 * There is no support for time zones, leap years, etc.
 * So only use this when you're certain the dates and times you're storing are generated with special cases in mind.
 * @author Simon
 *
 */
public class SimpleDateTime 
{
    int year;
    int month;
    int day;
    int hour;
    int minute; 


    /**
     * Construct a simple date and time object
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     */
    public SimpleDateTime(int year, int month, int day, int hour, int minute) {
        super();
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = hour;
        this.minute = minute;
    }

    /**
     * Construct a simple date object, with time component initialised to 0
     * @param year
     * @param month
     * @param day
     */
    public SimpleDateTime(int year, int month, int day) {
        super();
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = 0;
        this.minute = 0;
    }

    /**
     * Construct a simple time object, with date component initialised to 0
     * @param hour
     * @param minute
     */
    public SimpleDateTime(int hour, int minute)
    {
        super();
        this.year = 0;
        this.month = 0;
        this.day = 0;
        this.hour = hour;
        this.minute = minute;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }   

    public String toDateString()
    {
        return day + "/" + month + "/" + year;
    }

    public String toTimeString()
    {
        return hour + ":" + minute;
    }

    /**
    * Parses a date from a string to a {@link SimpleDateTime} object.
    * Can handle various delimiters, as in these examples:
    * <ul>
    * <li>15/10/1993</li>
    * <li>15-10-1993</li>
    * <li>15.10.1993</li>
    * </ul>
    * It can also handle mixed delimiters as in these examples:
    * <ul>
    * <li>15/10.1993</li>
    * <li>15-10/1993</li>
    * <li>15.10-1993</li>
    * <li>etc.</li>
    * </ul>
    * @param date the string to parse
    * @return
    */
    public SimpleDateTime parseDate(String date)
    {
        String[] dateParts = date.split("[-\\.:]");
        int day = Integer.parseInt(dateParts[0]);
        int month = Integer.parseInt(dateParts[1]);
        int year = Integer.parseInt(dateParts[2]);

        return new SimpleDateTime(year, month, day);
    }




    /**
    * Parses a time from a string to a {@link SimpleDateTime} object.
    * Can handle various delimiters, as in these examples:
    * <ul>
    * <li>20.07</li>
    * <li>20:07</li>
    * <li>20-07</li>
    * </ul>
    * @param time the string to parse
    * @return
    */
    public SimpleDateTime parseTime(String time)
    {
        String[] timeParts = time.split("[-\\.:]");
        int hours = Integer.parseInt(timeParts[0]);
        int minutes = Integer.parseInt(timeParts[1]);

        return new SimpleDateTime(hours, minutes);
    }
}
Community
  • 1
  • 1
Simon Verbeke
  • 2,905
  • 8
  • 36
  • 55
0

I have this helper classes:

https://gist.github.com/feandrad/06f0e00538e245a2afb3787d4144e034

  • DateTime.java Static class with some common operations
  • SimpleDate.java: Class that handles Year, Month, Day of the month (Support leap year)
  • SimpleTime.java Class that handles Hour, Minute, Seconds, Milliseconds

I made those classes some time ago, hope it helps someone. Be free to contribute if you want to.

Felipe Andrade
  • 509
  • 6
  • 23