I am very new to java and I have been assigned a task in which I have to select single date (01-01-2011) or range of dates like (from 01-01-2011 to 22-03-2011)along with time, Time can also be optionally selected.
I was previously a web developer and there are a lot of date or time range picker available there and they are very easy to customize. But in JAVA, everything seems complex.
I have seen some examples on internet so far like LGoodDatePicker BUT I don't have any idea how to implement this.
Any tutorial or straight guideline will help me alot.
Thanks
LGoodDatePicker is great, I am also using it. I would suggest you to stick to, what is widely used to get help.
LGoodDatePicker has demo code in GitHub repo. I used those example codes to figure out how to implement it. You can compare those to the screenshots provided here.
Generally, you can create a component with something like this:
DatePickerSettings datePickerSettings = new DatePickerSettings();
datePickerSettings.setFormatForDatesBeforeCommonEra("dd.MM.yyyy");
datePickerSettings.setFormatForDatesCommonEra("dd.MM.yyyy");
TimePickerSettings timePickerSettings = new TimePickerSettings();
timePickerSettings.use24HourClockFormat();
DatePicker datePicker = new DatePicker(datePickerSettings);
panel.add(datePicker);
datePicker.setDateToToday();
A range is nothing but a start and end date. You could use 2 such components for a start date and an end date. You need to check that start date is before end date.
Related
I am looking for the most simple and cleanest way to fix the timezone for all dates in an Android app. The idea is to have the app running as if the user were in another timezone. Let me clarify what I am looking for:
Let's say the user's phone is set to America/New_York then I would like my app to show all dates (are in UTC) in the Europe/Amsterdam timezone, regardless of the timezone that is set on the phone itself. And if I make a comparison with a new Date() it would be very nice if that new Date() is also in the current time of the Europe/Amsterdam timezone.
After searching the internet for solutions, I started to get the feeling that I will have to update every place in my app where a Date is used and force the use of the target timezone, like the solution of this stackoverflow post: Converting UTC dates to other timezones
Does anybody know how to get this done in a more easy and cleaner way?
The answer for anyone using java.time, the modern Java date and time API.
java.time does not include an option for setting the JVM default time zone. And wisely so. It’s not something you should want to do. By doing it you affect every program running in the same JVM, and also every part of your program and other program in the JVM may set it differently, ruining your intentions.
Avoid the need
In your time operations be explicit about which time zone you want, and you will always know what you get independently of the JVM setting. Example:
System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Dushanbe")));
Example output:
2021-05-09T00:36:25.171213+05:00[Asia/Dushanbe]
System.setProperty
If you have already written a lot of code relying on the default time zone of the JVM, the hack to set it is:
System.setProperty("user.timezone", "Australia/Tasmania");
System.out.println(ZonedDateTime.now());
This just printed:
2021-05-09T05:38:03.568350+10:00[Australia/Tasmania]
It’s not very robust, though, for the reasons mentioned in the beginning.
If you want validation of the string you are passing, use:
System.setProperty("user.timezone", ZoneId.of("Australia/Tasmania").getId());
Disclaimer
It seems from your question that you are already using the old, poorly designed and long outdated java.util.Date class and friends. I still wanted to post the answer for users who have the option to go modern. (You may also use each of the two ideas presented with the out-dated API.)
I would try TimeZone.setDefault(TimeZone) like in:
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Amsterdam"));
(advice to check returned time zone, getTimeZone does not throw exception for unknown time zone - or use ZoneId instead of the String)
see TimeZone (this also mentions the user.timezone system property)
but I am not an android programmer/user
I want to create a UI to enable the user to set specific configuration from date1 to date2. To pick the dates I want the calendar to pop up.
To create the calendar, I can put a combo box to choose the month, along with 30 buttons as representative of the days (enabling/disabling some days based on the selected month).
If you know a better way to do this, I would be very thankful if you share it with me!
Thanks!
Please don't do it like that. You have the full power of Java at your hands. There are many Java libraries for date pickers like this one:
https://toedter.com/jcalendar/
Download the .jar file and add it to your model dependencies. Then you can create the date picker object using the respective API.
Here is an AnyLogic example model but it currently only works in AnyLogic 7 (since 8 massively changed how things are displayed). To get it working in AL 8, you might need to ask AL support.
I want to make a user pick a date. After this, I want to convert it to a java.time.LocalDate without getting in trouble.
I would like to get something looking like this:
I know, this image is made from a JDatePicker. I read several tutorials about this, but it seems to be outdated or no longer supported – eclipse tells me it cannot import JDatePickerImpl (for example). (I tested that class by trying this tutorial (look for the answer by MadProgrammer)).
What do I search for? (I guess it already exists, I wonder if I had to implement it myself.)
How to use it? (Link for a good tutorial, for example)
setting date (using java.time.LocalDate)
getting date (using java.time.LocalDate)
I need to use "big" dates: I would like to make it work with at least 2000 years from now. For this purpose it's important to make the user able to type the year manually. (Instead of clicking up and down, but in the image it looks like this is possible.)
I am not sure if I understand all your specs well but at least I can share tutorial from Oracle docs which worked fine for me with Java8 (JavaFX) when I experimented with the date.
JavaFX Date Picker
Hope it helps.
I have a TextClock in android that is supposed to be displaying the time from the system in 12-hour format only. The problem is it is displaying it in 24-hour format even when the system itself is displaying it in 12-hour format.
I have already used the android:format12Hour="HH:mm:ss" in my text view, but it seems to be completely ignoring this.
And I know android guidelines say to not overrride user's system setting, but this textclock is for an app that is specifically designed for mathematical calculations in oil drilling industry that need it displayed in 12-hour format.
Joop Eggen's solution worked.
In java SimpleDateFormat HH = 24h, hh = 12h
Not sure how I overlooked that. :) Thank you!
I just had this problem, solved it like this:
if (sharedPreferencesSingleton.getUserPreferences24HourMoodBoolean()){
textClock.setFormat12Hour("HH:mm");
textClock.setFormat24Hour("HH:mm");
} else {
textClock.setFormat24Hour("hh:mm a");
textClock.setFormat12Hour("hh:mm a");
}
This overides the format no matter what the default user setting is.
android:format12Hour specifies format to be used when TextClock is using 12hrs mode but it does not enforce it:
Specifies the formatting pattern used to show the time and/or date in 12-hour mode.
so if user's device is using 12hrs clock mode, then that format will be used, otherwise android:format24Hour is used.
in oil drilling industry that need it displayed in 12-hour format.
Pardon my French, but that's usually pure BS. How time calculations are affected by the way you display it? Regardless of what clock mode user uses your result will be the same. If not, then your app is broken an that should be fixed.
EDIT
TextClock is pretty simple widget. If current implementation does not fit your needs, write your own. You can even reuse current code TextClock.java
http://i41.tinypic.com/344bo77.png
From Day Cannot be Today's Date or Later.
To Date cannot be maximum than today's date & From Date.
I have to set the code in such a way that after selecting a particular date on From date, To date will be enabled.
Sample code will be helpful.
I would recommend you use Joda Time for your date calculations. With regards to enabling and disabling your date values, Swing components have setEnabled() methods which you can use to either disable or enable the component.
That being said, you did not specify what have you tried... and are requesting code... I doubt those are two steps in the right direction for using this site...