I am developing a basic contact address application using Java FX.
I am using LocalDateTime and I have also created a class to format the date and time into a String so I can display it in Label of Java FX.
However, I do not understand how can I display the date and time when I create a new contact or when I edit one that already exists (I have already implemented all the functions I have just described, except the one I am asking right now).
At the moment I have managed only to enter manually a date and a time via GUI, but that is not what I want.
If you need any other relevant information or some snippets of the code to help me faster, just ask and I will edit the post.
If I understood the question correctly, just generate a date in your method that creates or edit the contact.
String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
You can also use timezones.
String currentDate = LocalDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"));
Read about java date here: http://www.baeldung.com/java-8-date-time-intro
Related
I am a beginner in Java programming and I am using Netbeans. I want to change the Date format of the Microba date picker to "dd-MMM-yy" and also remove the remaining fields. How to do these things?
Thank you.
Since Microba DataPicker is open source you could just clone the repo and have a look at the source code for your self.
If you have a look at the source code for the DatePicker class you will notice a number constructors which you can use to customise the picker, several take a DateFormat class which you can use to modify the format of the date value.
If you don't know how to use DateFormat, then I recommend you take a look at SimpleDateFormat, there are countless examples available to show you how to use it
As to remove elements, you'd have to look closer at the source code and make determinations on how best that might be achieved, since I'm not sure what fields you're talking about.
This question already has answers here:
How can I ensure in Java that the current local time is correct? [duplicate]
(3 answers)
Closed 6 years ago.
I stumbled upon a idea below and wanted to know your opinion:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
If the user's computer is set to a wrong date, does this is print out wrong date.
From where does the JVM captures the date ? How to make sure that Date is always printed correctly even when the end user has adjusted his date?
java relies on the system clock, so if the user has misconfigured their time, timezone, date etc that is what you'll see.
having said that, if your code can assume a working internet connection you could maybe use some java NTP implementation to connect to internet time servers and get the correct time.
see more info here - How to use an Internet time server to get the time?
if you dont require the degree of precision that NTP offers and want something simpler you could hit up any number of rest APIs that serve time - here is one
JVM will capture the date from the System. If you set it incorrect it will give you incorrect timings. Still if you would like to have correct timings, you can use any REST API's which will pick up correct date, time according to your timezone from the internet.
You can get free APIs from TimeZone
I am writing a Java program using selenium. I need a function that returns a unique date (mm/dd/yyyy) each time it is called. The conditions though are that
It can never return a date it returned before
Is must return a date between 01/01/2071 and 12/31/9999
The program will run many times so all program memory will be lost upon termination. It
must remember the dates it has returned before. See next item
The easiest way to do this is just keep incrementing the date by 1 day each time, so it
needs to remember only 1 date.
Unfortunately I cannot write the last date returned to a file in the system to read it
next time the program runs because I do not have that ability.
The program will be reading data from an Excel spreadsheet so could theoretically store
the latest date in a cell, but the spreadsheet will be open and it does not seem to
have the ability to write to an open file.
Any thoughts? One thing I thought about doing was using a base date like 1/1/2014 at 00:00:00 and then taking the current date, calculating the number of minutes between the two, and adding this as a number of days to 11/31/2070. Unfortunately this would work only a couple of years because then there would be more minutes between the two dates than there are days from 1/1/2017 to 12/31/9999
Any help is appreciated.
Thanks
I have tried the following so I can get Date based on my timezone which is "Africa/Johannesburg" or GMT+2:00 but Google servers always return time using its own timezone which is 2 hours behind mine.
I have done the FF:
in appengine-web.xml I have set
<property name="user.timezone" value="Africa/Johannesburg"/>
I have also tried TimeZone.setDefault(TimeZone.getTimeZone("GMT+2:00")); before creating Date object
in the init method of my servlet, I have also tried
#Override
public void init() throws ServletException {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+2:00"));
}
But this thing won't just work. Because JDK date is not thread safe, I am using JodaTime, which works well, In fact when I do new DateTime(DateTimeZone.forID("Africa/Johannesburg")) I get correct time but for legacy issues, I have to store date in JDK date hence have to convert Joda to JDK Date by invoking .Date(), then the time is completely screwed up in wrong timezone.
Does anyone by chance know how to set this without having to subtract the hours difference.
You can't. The system timezone is not changeable. You should store all of your dates in unix time and convert them to a Date or Calendar object using your timezone. I also would not assume that GAE is always going to use the same timezone...
When you save any date in Datastore it will be saved in the timeZone you have set in your JVM, thats why before starting the app I always set it to UTC:
//To avoid difference of dates depending on where the server is located
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Nonetheless when you browse the datastore in the gcloud console it will be shown in your local timezone (probably it gets the browser timezone and adapts the response to you). But when you query it back the calendar date taken in count will be the one you used for saving it (In my case UTC).
I'm currently working on a Swing app and I've got a few JTextAreas that are going to be parsed, turned into dates and then added to a MySQL database. One is a Date field, the others are DateTime, what I'm trying to do is use InputVerifier to make sure they're entered correctly.
I've created an InputVerifier that tries to turn the text into a date useing DateFormat.parse() and that mostly works, however there are two flaws:
Firstly it can't check if I only have a Date, rather than a date and time
Secondly it can't check if I have both a date and time, rather than just one of them.
Is there any way around this? Or a better way to validate date fields in Swing I'm not aware off?
Swing has support for entering dates into text fields using a JFormattedTextField with a DateFormatter. With a DateFormatter you can specifiy a DateFormat object so can have either a date only field or a date and time field.
Have a look at How To Use Formatted Text Fields in the Swing tutorial as an introduction.
Just a thought: you could consider using a SpinnerDateModel with a JSpinner, instead of a text field. Maybe you could handle the date-only field by calling setCalendarField(Calendar.DAY) - never done it myself, though, so I'm not sure.