prevent user picking a future time in blackberry + java - blackberry

Can you please tell me how to set a date field to not allow future times to be chosen in blackberry + java?
The user is able to set future times, but I need the user to only be able to set past or current times - not future. Is it possible in blackberry + java?
if (field.equals(datePickerBtn)) {
final DateTimePicker dateTimePicker = DateTimePicker
.createInstance(Calendar.getInstance(), /*"dd:MM:yyyy"*/null, "HH:mm");
Calendar maxCalendar = Calendar.getInstance();
maxCalendar.set(Calendar.YEAR, 2013);
dateTimePicker.setMaximumDate(maxCalendar);
Calendar minCalendar = Calendar.getInstance();
minCalendar.set(Calendar.YEAR, 1900);
dateTimePicker.setMinimumDate(minCalendar);
calendar = Calendar.getInstance();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
if (dateTimePicker.doModal()) {
calendar = dateTimePicker.getDateTime();
Date date = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd:MM:yyyy HH:mm");
selectedDate = dateFormat.format(date);
dateEditTxt.setText(selectedDate);
}
}
});
}

The accepted answer given is not correct (answer fixed). BlackBerry Java does not support the full set of standard Java APIs, including Calendar#clear(). So, that code won't even compile.
Here is the relevant API documentation you need to use, which you may need to adjust if you're supporting a lower version of BBOS than 7.0.
Your original code is nearly correct already. Just remove a couple of lines to give:
final DateTimePicker dateTimePicker =
DateTimePicker.createInstance(Calendar.getInstance(),
/*"dd:MM:yyyy"*/null,
"HH:mm");
Calendar maxCalendar = Calendar.getInstance();
dateTimePicker.setMaximumDate(maxCalendar);
Calendar minCalendar = Calendar.getInstance();
minCalendar.set(Calendar.YEAR, 1900);
dateTimePicker.setMinimumDate(minCalendar);
In your original code, you were hardcoding the maximum year value to 2013, which is not going to work correctly after one more month.
You didn't specify exactly how you wanted to determine the minimum date, so I can't comment on whether or not you need to set additional fields (besides year = 1900) in your minimum date.

When you call Calendar.getInstance(), the returned Calendar will be set to the current time and date. This, in your case, could be made your maximum with:
dateTimePicker.setMaximumDate(Calendar.getInstance());
This should set your DateTimePicker maximum to the current time.
If you want to exert more control over the maximum date value, you can use Calendar#set(int, int) to fine tune the Calendar object, just as you've done above.
For example, to create two Calendar objects, maximum for the current time and minimum for the actual minimum time that Calendar is capable of representing, and display their values:
Calendar minimum = Calendar.getInstance();
Calendar maximum = Calendar.getInstance();
minimum.set(Calendar.HOUR_OF_DAY, 0);
minimum.set(Calendar.MINUTE, 0);
minimum.set(Calendar.SECOND, 0);
minimum.set(Calendar.MILLISECOND, 0);
// For Calendar.DAY_OF_MONTH, 1 is the actual minimum.
// 0 will underflow to the last day of the previous month.
minimum.set(Calendar.DAY_OF_MONTH, 1);
// For Calendar.MONTH, months are numbered 0 (Jan) through to 11 (Dec).
minimum.set(Calendar.MONTH, 0);
// For Calendar.YEAR, both 0 and 1 represent year 1CE.
minimum.set(Calendar.YEAR, 1);
See also: Java ME Oracle documentation for Calendar.

Related

Refresh the material calendar

A problem with refresh the mat-calendar image. an initial current month of the calendar was displayed, but if I select any date of next month then the mat-calendar will not update with the next month.
expected result: select any date, mat calendar jump, or shows to the selected date
I try How to refresh mat-calendar after changing the background of highlighted dates also but still phasing with the above problem.
I also encountered this problem. Here is a solution (here I use moment.js. Date works just as well)
HTML
<mat-calendar #smallCalendar
[startAt]="smallCalendarStartAt"
[selected]="smallCalendarSelected">
</mat-calendar>
Typescript
// Small calendar properties
#ViewChild('smallCalendar', { static: false }) smallCalendar: MatCalendar<Date>;
smallCalendarStartAt: Date;
smallCalendarSelected: any;
// Refresh the small calendar
this.smallCalendarStartAt = new Date(moment().year(), +moment().format('MM'), +moment().format('DD'));
this.smallCalendarSelected = this.smallCalendarStartAt; // Update the selected day in mat-calendar
this.smallCalendar._goToDateInView(this.smallCalendarStartAt, 'month'); // Update the month in the mat-calendar
You can adapt the variable smallCalendarStartAt. Here, for the example, it will only switch to the next month with the day of the actual month
Here is a demo with the Date function: DEMO

How to More Elegantly Calculate Epoch Time for Next Sunday at Midnight in Dart?

When a user opens my app, there's a countdown timer that shows how much time left until Sunday at midnight (which is when the week's contest would end).
To get the initial value used in the countdown, my code adds 604800000 (which is the amount of milliseconds in a week) in a loop to a starting value of 1595203200000 (which the milliseconds since epoch of an arbitrary past Sunday at midnight) until it's greater than now:
int now = DateTime.now().millisecondsSinceEpoch;
int nextSundayAtMidnight = 1595203200000; // starting value is from arbitrary past Sunday at midnight
while (now > nextSundayAtMidnight) {
print('now is still greater than nextSundayAtMidnight so adding another 604800000 until it\'s not');
nextSundayAtMidnight += 604800000;
}
print('nextSundayAtMidnight is $nextSundayAtMidnight');
It works, but it seems like there should be a better way that's based on DateTime.now without having to manually specify that arbitrary starting value. Is there?
What's the syntax in dart to do this more elegantly?
Thanks in advance!
The following code uses addition of the difference in weekdays to get the date of the upcoming Sunday and then shifts the exact DateTime to being at 11:59:59 pm. There are comments in the code that describe what each line does.
It uses the many helpful methods already provided in the DateTime class in dart.
void main()
{
var now = DateTime.now();
//Obtains a time on the date of next sunday
var nextSunday = now.add(Duration(days: DateTime.sunday - now.weekday));
//Shifts the time to being 11:59:59 pm on that sunday
var nextSundayMidnight = DateTime(nextSunday.year, nextSunday.month, nextSunday.day + 1).subtract(Duration(seconds: 1));
//Gets the difference in the time of sunday at midnight and now
var timeToSundayMidnight = nextSundayMidnight.difference(now);
print(timeToSundayMidnight);
}

How to format the Date to British Date or US Date and get time from DatePicker

New to MatBlazor below
<MatDatePicker #bind-Value="#Date1"></MatDatePicker>
Want to learn how to get date, format the date and get the time
Does this one has time component?
Thanks
Just get the value from variable Date1 from your component.
Example :
<MatDatePicker #bind-Value="#Date1"></MatDatePicker>
#code{
var formattedValue = Date1.ToString("dd mm yyyy");
var dateValue = Date1.Date;
var timeValue = Date1.TimeOfDay;
}
As the current version, the control have implemented ToLocalTime() default.
Please ask 1 question at a time.
MatDatePicker is based upon mat-datepicker
Best would to look into material design and the ui components for answers to your question
Even though it is for Angular, it gets close, for example look at Angular 6 material: how to get date and time from matDatepicker?

Grails Date Contraints

Im trying to add constraints to a user submitted text field, which is there Date of Birth. I need the user to be at least 18 but cant be over 113 years old.
final static Date MIN_DATE = new Date(Calendar.YEAR-18)
final static Date MAX_DATE = new Date(Calendar.YEAR-100)
static constraints = {
dob(nullabe: false, min: MIN_DATE, max: MAX_DATE
}
When I,
System.out.println('Year Max Date: ' + person.MAX_DATE)
it gives me,
Year Max Date: Wed Dec 31 17:59:59 CST 1969
and so does the min date.
I tried doing
final static Date MIN_DATE = new Date().getAt(Calendar.YEAR)-18
but that didn't work at all. I also tried doing it without the -18 and got the correct answer but it was 7 hours off. When I tried to relaunch the app from local it crashed it. And I cant recreate it ever since.
I have come to the understanding that the date its giving is the "Epoch" date or the date right before Unix was launched. Im just not sure how to get around it.
Any ideas/suggestions/concerns/experience with this problem?
Any Help would be greatly appreciated.
I don't agree with these constraints, firstly as they seem arbitrary (why should someone not be older than 100? And why can't I just lie if I am 16?)
And secondly, if the webserver is up for a couple of years, these static dates will slowly drift
But anyway (these concerns aside), I believe what you want is:
final static Date MIN_DATE = Calendar.instance.with { add( YEAR, -18 ) ; it }.time
final static Date MAX_DATE = Calendar.instance.with { add( YEAR, -100 ) ; it }.time
The issue is that Date constructor expects an argument representing milliseconds since January 1, 1970, 00:00:00 GMT. Since Calendar.YEAR is a constant that is used to represent the year field from a Calendar structure and equal to 1 you are getting the above values.
You need to convert the user submitted text field (a String) to a Date object with SimpleDateFormat. Then create a Calendar with the Date object and check the Year field.
Alternatively you can just parse the text field (String) yourself and pick the year, convert to int and do the check.

Problem in getting DST time?

I am trying to get the time of other GMT value by using
Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone(gmt));
but how can i get the DST time at that time zone.
The TimeZone class provides a getDSTSavings() method for a specific TimeZone Object. (JavaDoc: "Returns the amount of time to be added to local standard time to get local wall clock time.")
The Calendar interface provides two getOffset() methods, which let you find out the offset from UTC. (JavaDoc: "Returns the offset of this time zone from UTC at the specified date. If Daylight Saving Time is in effect at the specified date, the offset value is adjusted with the amount of daylight saving. ")
please see this piece of code to grok the complicated ways of java time:
#Test
public void testDST() {
final TimeZone met = TimeZone.getTimeZone("MET");
Calendar gc = new GregorianCalendar(met);
final long timeInMillis = gc.getTimeInMillis();
final long gmtTime= timeInMillis-(gc.getTimeZone().getOffset(timeInMillis));
final Date gmtDate = new Date(gmtTime);
System.out.printf("%-40s: %tc\n%-40s: %tc\n%-40s: %tc\n%-40s: %d\n%-40s: %d",
"new Date() (local timezone)",new Date(),
"UTC", gmtDate ,
"now from Calendar with TC GMT+02:00",gc,
"zoneoffset",gc.get(Calendar.ZONE_OFFSET),
"dst savings",met.getDSTSavings());
}
You can also define your own SimpleTimeZone and provide custom DST rules, however, i have not found out how to get this information from the predefined TimeZones.
You should also be aware, that if TimeZone.getTimeZone(TZName) does not find the specified timezone, it does not throw an exception, but it just uses GMT, which can cause major misunderstandings.
You can find all this information (and a lot more) in javadoc for Calendar, TimeZone, Date, etc.
There are few methods available in java.util.TimeZone to get Daylight Saving Time. Please check out the BlackBerry Java Docs page.

Resources