I have a textfield with a binding to a timestring like this:
timeStringProperty.bind(clock.getTimeString());
timeTextField.textProperty().bind(timeStringProperty);
In Clock.java I have:
public StringBinding getTimeString() {
return Bindings.createStringBinding(() -> DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM).format(time.get()), time);
}
where time comes from a TimeLine.
This works well and the current time is displayed and updated in the timeTextfield.
Now I want to add the ability for the user of my program to determine the format of the time e.g. "hh:mm" or "hh:mm:ss".
How can I bind to a formatString, such that the time display updates with the new format as soon as the formatString value is changed?
Introduce an ObjectProperty<DateTimeFormatter> to Clock:
public class Clock {
private final ObjectProperty<DateTimeFormatter> formatter =
new SimpleObjectProperty<>(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM));
public ObjectProperty<DateTimeFormatter> formatterProperty() {
return formatter ;
}
public final DateTimeFormatter getFormatter() {
return formatterProperty().get();
}
public final void setFormatter(DateTimeFormatter formatter) {
formatterProperty().set(formatter);
}
// ... other code as before
}
And now update the StringBinding so that it uses the formatter and binds to it:
public StringBinding getTimeString() {
return Bindings.createStringBinding(() -> getFormatter().format(time.get()), time, formatter);
}
Related
Suppose I want to convert a UNIX integer into DateTime Format when called upon.
(That's just an example)
I want to create a function just like .toUpperCase(); , .toString(); where function takes that particular item as argument without writing inside brackets. I did try going inside these default core functions but didn't help.
How do I do that for custom functions?
right now I'm here:
//------------- main()----------//
void main() {
int unixDT = 1619691181;
// print(unixDT.toDateString()); //<-----this is what I'm looking for
}
//------- This is a function that should be in another-separate dart file-------//
String toDateString(int dt) {
DateTime datetime = new DateTime.fromMillisecondsSinceEpoch(dt);
return datetime.toString();
}
If you need clarification, please comment down. This is my very first question on StackOverflow so I apologize for mistakes if any.
Sounds like you are looking for extension methods:
void main() {
const unixDT = 1619691181;
print(unixDT.toDateString()); // 1970-01-19 18:54:51.181
}
extension IntToDateStringExtension on int {
String toDateString() => DateTime.fromMillisecondsSinceEpoch(this).toString();
}
You can use extension methods for this situations. You can add a method in Integer. For more details https://dart.dev/guides/language/extension-methods
You can try to use this to refer to same object that is calling that method, Hope that will help
You should use extension methods
Example:
extension ExtendedDynamic on dynamic {
double toDouble() {
final sValue = this?.toString() ?? '';
final double parsedValue = double?.tryParse(sValue) ?? 0.0;
return parsedValue;
}
}
void someMethod() {
final dynamic test = 0.0;
print(test.toDouble());
}
In your case it will be like this:
extension ExtendedInt on int {
String toDateString() {
final dateString = 'your code...';
return dateString;
}
}
void someMethod() {
int unixDT = 1619691181;
print(unixDT.toDateString());
}
Important thing : Wherever you want to use this extension, you must import it
import 'package:yourProject/extensions/int_extention.dart';
I want to use Date and Time picker at the same time. But i can't get value of Time from Xamarin DatePicker just get Date value correctly.
I shared codes below
CustomDatePicker.cs
public class CustomDatePickerRenderer : DatePickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if(Control != null)
{
UITextField entry = Control;
UIDatePicker picker = (UIDatePicker)entry.InputView;
picker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
picker.MinimumDate = Foundation.NSDate.Now;
picker.MaximumDate = Foundation.NSDate.Now.AddSeconds(172799);
picker.Mode = UIDatePickerMode.DateAndTime;
}
}
}
Xaml
<DatePicker x:Name="datePickerIOS" DateSelected="datePickerIOS_DateSelected" ></DatePicker>
Xaml.cs
private void datePickerIOS_DateSelected(System.Object sender, Xamarin.Forms.DateChangedEventArgs e)
{
var date = datePickerIOS.Date;
// This date's time value always return 12:00:00 AM
}
As you use DateSelected="datePickerIOS_DateSelected" method,it was the event of the datepicker,you could only get the correct date.
You should get the correct date and time in your customrenderer and return it to your page(you could use MessagingCenter to achieve this).
You could refer to this link.
Please see content below, confused on what to put for return that has type Date
#Override
public int maxCapacity() {
return 3;
}
#Override
public String area() {
return "10";
}
#Override
public Date whenBuilt() {
return ***[What should I return here?]***
}
Date methods must to return Date instance or null..
You can just return new Date();, but it will always fresh date.
Method whenBuilt looks like some date in the past, so you should to make some date, in constructor for example.
class A {
private final Date createdAt;
A() {
createdAt = new Date();
}
public Date whenBuilt() {
return createdAt;
}
}
#Override annotation points to your class is extension from some base class.
Try to check base class, may be built date is already in there.
I have "2011-12-05" and I want to convert this to "Monday 05-Dec-2011".
My date conversion code depends on the device timezone. If my timezone is India, then I get date Monday 05-Dec-2011 and if my timezone is Kingston, Jamaica, I get Sunday 04-Dec-2011.
For this reason my application does not display the correct date for different timezones.
Is there any solution to convert date without Blackberry Date class or using current Date and Time Zone?
I want to only convert this date to String
I am converting this date using below function
public static String reformatMonthDate(String source)
{
SimpleDateFormat write = new SimpleDateFormat("dd MMM yyyy"); //YYYY-MMM-dd
Date date = new Date(HttpDateParser.parse(source));
return write.format(date);
}
You can specify a specific locale, instead of relying on the system's default.
Locale locale = Locale.get(Locale.LOCALE_fr);
// Parse with HttpDateParser
Date date = new Date(HttpDateParser.parse("2002-01-29"));
// Format with a custom format and locale
DateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy", locale);
StringBuffer buf = new StringBuffer(30);
String s = formatter.format(date, buf, null).toString(); // mar., 29 janv. 2002
DateTimePicker gives the current date according to the location or device configuration settings.
Try this code:
You should get your requirement.
public class LoadingScreen extends MainScreen implements FieldChangeListener
{
private String select_Date[]={"Select Date"};
private String month_ar[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
private ObjectChoiceField choiceField;
private ButtonField show;
public LoadingScreen()
{
createGUI();
}
private void createGUI()
{
choiceField=new ObjectChoiceField("Select Date: ", select_Date, 0)
{
protected boolean navigationClick(int status, int time)
{
DateTimePicker datePicker = DateTimePicker.createInstance( Calendar.getInstance(), "dd/MM/yyyy",null);
datePicker.doModal();
Calendar cal1=datePicker.getDateTime();
String day="";
String mon="";
if(String.valueOf(cal1.get(Calendar.DAY_OF_MONTH)).length()==1)
{
day="0"+String.valueOf(cal1.get(Calendar.DAY_OF_MONTH));
}
else
{
day=String.valueOf(cal1.get(Calendar.DAY_OF_MONTH));
}
mon=String.valueOf(cal1.get(Calendar.MONTH));
String month=""+month_ar[cal1.get(Calendar.MONTH)];
select_Date[0]=day+"-"+month+"-"+cal1.get(Calendar.YEAR);
choiceField.setChoices(select_Date);
return true;
}
};
add(choiceField);
show=new ButtonField("Show",FIELD_HCENTER);
show.setChangeListener(this);
add(show);
}
public void fieldChanged(Field field, int context)
{
if(field==show)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(select_Date[choiceField.getSelectedIndex()]);
}
});
}
}
public boolean onMenu(int instance)
{
return true;
}
protected boolean onSavePrompt()
{
return true;
}
}
Default calendar widget of Smartgwt Component shows date as Sun 10/31 ( MM/DD format ) .
http://www.smartclient.com/smartgwt/showcase/#simple_calendar_category
How can I change the date format to DD/MM formatin Calendar Widget ?
Thanks in advance for having a look,
Sachin
SmartGwt provides a static DateUtil for changing date formats.Put the following code somewhere in your application and it will change the date format through out the application
DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
public String format(Date date) {
if(date == null) return null;
final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("DD/MM/yyyy");
String format = dateFormatter.format(date);
return format;
}
});
Take a look to the com.smartgwt.client.util.DateUtil specifically to the methods setShortDateDisplayFormat and setShortDateDisplayFormatter
This should solve your problem, according to the documentation.
put
this.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
in constructor
#aruns is correct, however another step is need if you are using RelativeDateItem
DateUtil.setShortDatetimeDisplayFormatter(new DateDisplayFormatter()
{
public String format(Date date)
{
if(date == null)
{
return null;
}
else
{
final DateTimeFormat dateFormatter = DateTimeFormat.getFormat(DATE_FORMAT);
return dateFormatter.format(date);
}
}
});
DateUtil.setDateParser(new DateParser()
{
public Date parse(String dateString)
{
final DateTimeFormat format = DateTimeFormat.getFormat(DATE_FORMAT);
return format.parse(dateString);
}
});
The DateFormat is defined in the GWT documentation