• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Comparing Dates & Timestamps
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Comparing Dates & Timestamps


  • Subject: Re: Comparing Dates & Timestamps
  • From: David LeBer <email@hidden>
  • Date: Thu, 11 Mar 2004 17:07:02 -0500

On 11-Mar-04, at 4:12 PM, Art Isbell wrote:

On Mar 11, 2004, at 9:49 AM, David Griffith wrote:

Now I want to fetch or filter records where:

OrderDate = todaysDate

Can someone tell me an easy way? I have been fiddling with calendars etc but
not having much luck. I know I can get a new NSTimestamp, but since this
includes the time also it will not match.

You could get a new NSTimestamp and remove the hours, minutes, and seconds, but that requires mucking with Java's very clumsy date classes as you've probably noticed. I think it's rarely safe to compare timestamps for equality. Instead, test that a timestamp is within a range of timestamps (untested pseudocode follows):


NSTimestamp today = new NSTimestamp();
NSTimestamp yesterday = today.timestampByAddingGregorianUnits(0, 0, -1, 0, 0, 0);
EOQualifier qualifier = EOQualifier.qualifierWithQualifierFormat("orderDate > %@ and orderDate <= %@", new NSArray(new NSTimestamp[] {yesterday, today});

This is normally what I do:

I add a CBTimestampUtility.java class to my project with these methods:

//
//  CBTimestampUtilities.java
//  World Altering Project
//
//  Created by David LeBer on Tue Feb 25 2004.
//  Copyright (c) 2004 Codebase Software Systems. All rights reserved.
//

import com.webobjects.foundation.*;
import com.webobjects.eocontrol.*;
import java.util.GregorianCalendar;

public class CBTimestampUtilities {

public static NSTimestamp startOfDay(NSTimestamp date) {
return CBTimestampUtilities.replaceTimestampTime(date, "0", "0", "0", "AM");
}


public static NSTimestamp endOfDay(NSTimestamp date) {
return CBTimestampUtilities.replaceTimestampTime(date, "11", "59", "59", "PM");
}


    public static int deltaToLocalTimeZone() {
        NSTimeZone tz = NSTimeZone.defaultTimeZone();
        int seconds = tz.secondsFromGMT();
        return seconds/60/60 - ((tz.isDaylightSavingTime())? 1 : 0);
    }

public static String hourStringFromTimestamp(NSTimestamp timestamp) {
GregorianCalendar myCalendar = new GregorianCalendar();
myCalendar.setTime(timestamp);
return (((myCalendar.get(GregorianCalendar.HOUR)) == 0) ? "12" :
String.valueOf(myCalendar.get(GregorianCalendar.HOUR)));
}


public static String minuteStringFromTimestamp(NSTimestamp timestamp) {
GregorianCalendar myCalendar = new GregorianCalendar();
myCalendar.setTime(timestamp);
String m = String.valueOf(myCalendar.get(GregorianCalendar.MINUTE));
if (m.length() == 1) {
return "0" + m;
} else {
return m;
}
}


public static String periodStringFromTimestamp(NSTimestamp timestamp) {
GregorianCalendar myCalendar = new GregorianCalendar();
myCalendar.setTime(timestamp);
return (((myCalendar.get(GregorianCalendar.AM_PM)) == 0) ? "AM" : "PM");
}


public static NSTimestamp replaceTimestampTime(NSTimestamp timestamp, String hr, String min, String sec, String period)
{
NSTimeZone tz = NSTimeZone.defaultTimeZone();
GregorianCalendar myCalendar = new GregorianCalendar();
myCalendar.setTime(timestamp);
int year = myCalendar.get(GregorianCalendar.YEAR);
int month = myCalendar.get(GregorianCalendar.MONTH) + 1 ;
int day = myCalendar.get(GregorianCalendar.DAY_OF_MONTH);
int hour = twoFourHourFromHourAndPeriod(hr, period);
int minute = Integer.parseInt(min);
int second = Integer.parseInt(sec);
return new NSTimestamp(year, month, day, hour, minute, second, tz);
}


    public static int twoFourHourFromHourAndPeriod(String h, String p) {
        int hour = Integer.parseInt(h);
        if ("AM".equals(p)) {
            return (hour == 12) ? 0 : hour;
        } else {
            return (hour == 12) ? 12 : hour + 12;
        }
    }

public static NSTimestamp timestampFromStringWithFormat(String time, String format) {
try{
if (format == null) {
format = "%m/%d/%Y";
}
NSTimestampFormatter formatter = new NSTimestampFormatter(format);
NSTimestamp timestamp = (NSTimestamp)formatter.parseObject(time);
return timestamp;
} catch (Exception e) {
e.printStackTrace();
}
return new NSTimestamp();
}



The following can be really useful when comparing dates:

CBTimestampUtilities.startOfDay(myTimestamp);

CBTimestampUtilities.endOfDay(myTimestamp);


;david

--
David LeBer
Codebase Software Systems
site:   http://www.codebase.ca
blog: http://david.codebase.ca
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Comparing Dates & Timestamps (From: David Griffith <email@hidden>)
 >Re: Comparing Dates & Timestamps (From: Art Isbell <email@hidden>)

  • Prev by Date: Re: Comparing Dates & Timestamps
  • Next by Date: Re: Comparing Dates & Timestamps
  • Previous by thread: Re: Comparing Dates & Timestamps
  • Next by thread: Re: Booleans
  • Index(es):
    • Date
    • Thread