Set first day of week in JDateChooser - java

I need some help to be able to set the first day of week from Sunday to Monday (change SMTWTFS to MTWTFSS) in com.toedter.calendar.JDateChooser, I tried like this with no result, I'm using version 1.3.3 of JDateChooser:
JDateChooser dateChooser = new JDateChooser(new Date());
dateChooser.getCalendar().setFirstDayOfWeek(Calendar.MONDAY);

Following the conventions of proper getter implementation, getCalendar() probably returns a copy of the calendar used. Therefore your call to setFirstDayOfWeek() is on an object that is not the calendar object of your JDateChooser.
I can't seem to find the documentation for JDateChooser 1.3.3, but if setCalendar() exists, this should work:
Calendar c = dateChooser.getCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
dateChooser.setCalendar(c);

Related

java- How to add 7 days to a default date?

I have a current date in Java like below:
String currentDate = CoreUtil.parseDate(new Date());
This returns the date for today in the form 2019-03-26.
I declared another date so that it should automatically add 7 days to the current date like below:
String defaultendDate=CoreUtil.parseDate(new Date()); + 7 days //example
So the defaultEnddate should be 2019-04-03
How would I accomplish this as I don't want to use any simple date formatter?
Also, I would like to store the date as it is in String for reasons and secondly, I only want date, not the time. I am not using Java 8 as well, so I can't really use LocalDate library here.
LocalDate is perfect for this job:
LocalDate.now().plusDays(7);
You can get your string representation with
.format(DateTimeFormatter.ISO_DATE);
If you're not able to use Java 8, then you have a few options:
Use the ThreeTen-Backport, which backports most functionality of the Java 8 JSR-310 API, normally available in the java.time package. See here for details. This package is available in Maven Central.
You can also use Joda Time. The peculiar thing is that these two projects have almost the same layout of their websites.
If you're otherwise not able to use ThreeTen-Backport or Joda Time, you can use this:
Calendar c = Gregorian​Calendar.getInstance();
c.add(Calendar.DATE, 7);
String s = new Simple​Date​Format("yyyy-MM-dd")
.format(c.getTime());
Warning
Many things are wrong with the old Date and Time API, see here. Use this only if you have no other option.
Use Calendar.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 7);
Date defaultEndDate = cal.getTime();
Something like
LocalDate.now().plusWeeks(1);
would also do the cause.
Please, bare in mind using Java 8 Date/Time API for any operations with dates and times. as it addresses shortcomings of old Date and Calendar regarding thread safety, code design, time-zone logic and other.
UPDATE:
If you must use old Date/Time API, following code would suffice:
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, 7);
System.out.println("Adding seven days: " + calendar.getTime());
date = calendar.getTime();
//your code
String currentDate = CoreUtil.parseDate(new Date());
*
String dt = new SimpleDateFormat("yyyy.MM.dd").format(new Date()); // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 7); // number of days to add
dt = sdf.format(c.getTime()); // dt is now the new date
System.out.println(dt);
*
Use java.util.Date try this one

Issue populating current date into the web form using selenium by reading from excel sheet

I have the code of getting current date, But i need Set current date -1 i.e yesterday's Date.
However my main issue comes how could I populate in to the webform using selenium , as i do have to read surname, forename, DOB and medicalreport (here i need to fill it with yesterday date in ddMMyyyy format) reads from Excel File.
Here the is java code to get the Yesterday's Date with format ddMMyyyy.
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(dateFormat.format(cal.getTime()));
Hope this help you.Let me know if this work.

Vaadin datefield component set a default date

My question is if I put a date field in my web application's UI using vaadin framework like following codes, then how can I set a default date value to it? I can also see the setValue() method suggestion coming with date field in eclipse editor but has not much information how to set a particular date with setValue() in its documentation.
DateField dueDate = new DateField();
dueDate.setDateFormat("dd-MM-yyyy");
dueDate.setVisible(true);
dueDate.setCaption("Due Date");
**dueDate.setValue();**
Thanks in advance for your concerns. I am using vaadin7.
It's indeed the setValue method you need to call. You can supply a java.util.Date object. The first code example on this documentation page shows it.
// Create a DateField with the default style
DateField date = new DateField();
// Set the date and time to present
date.setValue(new Date());
If you are on Java 8 and want to use a LocalDate or LocalDateTime, you need to convert it to java.util.Date or write a Converter for the DateField (I did that some time ago, see here).

how to add days to java simple date format

How should I add 120 days to my current date which I got using simple date format?
I have seen few posts about it but couldn't get it to work,
My code is below:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//get current date time with Date()
Date date = new Date();
Do I need to use the Calendar library or can I just do it with simple date format?
Basically, you can simple use a Calendar which has the capacity to automatically roll the various fields of a date based on the changes to a single field, for example...
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 120);
date = cal.getTime();
Take a closer look at Calendar for more details.
Yes, there is a way to do this using Joda Time, but I could type this example quicker ;)
Update with JodaTime example
The following is an example using JodaTime. You could parse the String value directly using JodaTime, but since you've already done that, I've not bothered...
Date date = ...;
DateTime dt = new DateTime(date);
dt = dt.plusDays(120);
date = dt.toDate();
I would suggest you use Joda DateTime if possible. The advantage is it handles TimeZone very gracefully. Here's how to add days:
DateTime added = dt.plusDays(120);
Reference:
http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#plusDays(int)

How to get milliseconds from a date picker?

In my project I am saving milliseconds in a sqllite databse, by default I am saving
System.currentTimeMillis()
in the database, but there is an option for the user to select previous date from a date picker? But what sould I save then when user selects a previous or up comming days from the date picker? How can I get that day as a long(milliseconds) format?
Create a Calender instance and set the date you want. Then call calendar.getTimeInMillis();. See the answer of this previous SO question for more information.
EDIT To set the calendar date you can use something like this:
//Lets suppose you have a DatePicker instance called datePicker
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
cal.set(Calendar.MONTH, datePicker.getMonth());
cal.set(Calendar.YEAR, datePicker.getYear());
See the Calendar class for more information.
I was looking for a similar solution for a javase scenario when I came across this answer. I made a little modification to Angelo's answer to make my own case work, and it gave me exactly what I was looking for. To make things a little more interesting, I created a method that can be reused.
class Controller {
public static Calendar cal = Calendar.getInstance();
public static long getEpoch_fromDatePicker( DatePicker datePicker ) {
Controller.cal.set(Calendar.DAY_OF_MONTH, datePicker.getValue().getDayOfMonth() );
Controller.cal.set(Calendar.MONTH, datePicker.getValue().getMonthValue() );
Controller.cal.set(Calendar.YEAR, datePicker.getValue().getYear() );
return Controller.cal.getTimeInMillis() /1000;
}
}
Someone might find this useful

Categories