PDA

View Full Version : Using TimeField to set Time on java.util.date



firejack
31 Aug 2008, 8:41 AM
I am trying to use a TimeField component to set the time on a java.util.Date object and am having some trouble finding a way to do it. Algorithmically, I know I need to parse the TimeField string and turn it into a time object (which presents some challenges due to the emulation - it seems). Then I need to set the time on the date object.

I am assuming the TimeField was intended for this type of use - so I am expecting I am being myopic in some way. :) Can someone suggest a way to approach this I may not have thought of?

Right now, I am just sending the time String and the Date object back to the server separately and doing all the re-assembly there. That just feels wrong to me. Call me anal retentive. :)

maku
31 Aug 2008, 12:44 PM
Here is a slightly enhanced TimeField I use because of the shortcomings of the base TimeField.

Maybe it helps you to handle your problem.




import java.util.Date;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.DateWrapper;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.TimeField;

/**
* Provides a time field with enhanced date handling capabilities
*
* <p>
* e.g. typical usage of a separated date and time input field which belong together
* </br></br>
* <code>
* final ExtTimeField timeField=new ExtTimeField();</br>
*
* final DateField dateField=new DateField();</br>
*
* </br>
* Button timeTestButton = new Button("Time Resolving");</br>
* timeTestButton.addSelectionListener(new SelectionListener<ComponentEvent>(){</br>
* public void componentSelected(ComponentEvent ce) {</br>
* System.out.println(timeField.getDateTimeValue(dateField.getValue()));</br>
* }});</br>
*
* </code>
*
* </p>
*/
public class ExtTimeField extends TimeField {

private Date date;

public ExtTimeField() {
this(null);
}

public ExtTimeField(Date date) {
setForceSelection(true);
setValue(date);
}

public void setValue(Date dateValue) {
if (dateValue != null) {
date = new Date();
date.setTime(dateValue.getTime());
BaseModelData timeAsModelData = new BaseModelData();
timeAsModelData.set("text", getFormat().format(date));
setValue(timeAsModelData);
} else {
date=null;
}
}

/**
* Returns a {@link Date} instance with time value set
*
* @param forDate
* date value for which the time will be set
* @return {@link Date} instance with time set when time is selected
* otherwise date with no time information
*/
public Date getDateTimeValue(Date forDate) {

Date resultDate = null;

String timeAsString = resolveTimeAsString();

if (timeAsString != null && timeAsString.length() > 0) {
Date selectedTime=null;

selectedTime = parseDateFromString(timeAsString);

// check if time shoult be set for an existing date instance
if (forDate == null) {
// no existing date instance -> therefore result is
// parsed date
resultDate = selectedTime;
} else {
DateWrapper selectedTimeWrapper = new DateWrapper(selectedTime);

// set selected time value for date
resultDate = new DateWrapper(forDate).clearTime().addHours(selectedTimeWrapper.getHours()).addMinutes(
selectedTimeWrapper.getMinutes()).asDate();
}

} else {
// in case of no selection -> return current date without time
// information
resultDate = new DateWrapper(forDate!=null?forDate:new Date()).clearTime().asDate();
}
return resultDate;
}

/**
*
* @param timeAsString
* @param selectedTime
* @return date instance from time string
*/
@SuppressWarnings("unchecked")
protected Date parseDateFromString(String timeAsString) {
Date selectedTime =null;
try {
// parse Date instance for time string
selectedTime = getFormat().parse(timeAsString);
} catch (IllegalArgumentException iae) {
// in case of parsing error
final Listener l = new Listener<ComponentEvent>() {
public void handleEvent(ComponentEvent ce) {
markInvalid(messages.getInvalidText());
focus();

}
};
MessageBox.alert("", messages.getInvalidText(),l);
}
return selectedTime;
}

protected String resolveTimeAsString() {
String timeAsString = null;
if (getForceSelection()) {
if (getSelection().size() != 0) {
ModelData selectedTimeAsModelData = getSelection().get(0);
// check selection of time info
if (selectedTimeAsModelData != null) {
// resolve time info as string
timeAsString = selectedTimeAsModelData.get("text");
}
}
} else {
timeAsString=getRawValue();
}
return timeAsString;
}

public Date getDateTimeValue() {
return getDateTimeValue(date);
}

}

paulsschwarz
4 Nov 2008, 1:19 PM
Thanks Maku, I found that useful and quick. With the newer versions of GXT markInvalid(string) is deprecated and needs to be replaced with forceInvalid(string). Other than that all I needed was a convenient timeField.setValue(Date) function and this worked nicely.