Is there a way to set the date value on a JDatePicker - java

I am making an edit page for a booking program in which one of the things the user can edit is a begin date and an end date of a certain booking. At the moment the user can select the booking and the info will be put into the JTextFields but not the JDatePickers (the ones used for the begin and end date).
Now it is:
But it should look something like this (I put in the dates manually):
but I'cant find out how to programmatically set the dates in a JDatePicker. I followed this tutorial and came up with this decleration for the JDatePickers, One for the enddate One for the begindate:
pBooking.put("text.today", "Today");
pBooking.put("text.month", "Month");
pBooking.put("text.year", "Year");
beginDatePanelBooking = new JDatePanelImpl(beginModelBooking, pBooking);
endDatePanelBooking = new JDatePanelImpl(endModelBooking, pBooking);
beginDatePickerBooking = new JDatePickerImpl(beginDatePanelBooking, new DateLabelFormatter());
endDatePickerBooking = new JDatePickerImpl(endDatePanelBooking, new DateLabelFormatter());
I also made a small example here which can be used to simulate the problem i'm having:

You can use JXDatePicker from SwingX. Tutorial: Introduction to the JXDatePicker.

Related

Read Anylogic schedule exceptions from database table

How can I read a list of exception dates for a schedule from an Excel file without having to adopt each date from the file separately? I am trying to set up a shift-plan which takes into account holidays, etc. over the next 5 years. For this I have created an Excel table containing a list of holiday dates, which I would now like to use in my AnyLogic simulation. I tried the exceptions section of my schedule object, but didn't find a way to connect this section to my excel file. The only option I am getting here is to manually enter each date... Since this would be extremely tedious, I am looking for a workaround (Java Code?). Can someone help?
Suppose you have a scheduled object of type integer and a database table full of exception dates that you want the integer value of the schedule to be 0 for the entire day.
You can then programmatically add exceptions to your scheduling with the following code
List<Tuple> rows = selectFrom(db_table).list();
for (Tuple row : rows) {
Date exceptionDate = row.get( db_table.db_column );
schedule.addException(exceptionDate.getYear(), exceptionDate.getMonth(), exceptionDate.getDay(), exceptionDate.getHours(), exceptionDate.getMinutes(), exceptionDate.getSeconds(),
exceptionDate.getYear(), exceptionDate.getMonth(), exceptionDate.getDay()+1, exceptionDate.getHours(), exceptionDate.getMinutes(), exceptionDate.getSeconds(),
0, false);
}
The format for adding exceptions is a bit cumbersome but it is:
schedule.addException(startYear, startMonth, startDay, startHour, startMinute, startSecond, endYear, endMonth, endDay, endHour, endMinute, endSecond, value, annually);
The value object is the value of your schedule type
True, there is no build-in way for this. You need to code your schedule. This allows adding exceptions programmatically, see https://anylogic.help/anylogic/data/schedule.html#creating-and-initializing-schedule-from-code-on-model-startup
Specifically:

Calculate difference between 2 times in MongoDB

I have a field called Last Modified At with value like '2016/04/12 20:24:18'. It is not an ISO Date but a normal String value stored via a java process in MongoDb.
I am trying to write a shell script to calculate the difference between '2016/04/12 20:24:18' and say '2016/04/12 16:24:18'. The difference could be either in days or hours or mins or secs. I tried couple of things including converting to ISO dates but it doesnt work out. Is there an easy way to find out like Oracle.
Any help would be appreciated?
Thanks,
Ram
I'm not exactly sure how you plan on running the shell script, but it is possible in the mongo shell to parse dates (using Javascript) and calculate time between two dates as you have asked. Assume we have a document in the things database as follows:
{
"_id" : ObjectId("570f1e528163383227ace761"),
"lastModifiedAt" : "2016/04/12 20:24:18"
}
We can run the following script to get the difference between the lastModifiedDate of a document and a hard-coded date, such as 2016/04/12 16:24:18:
db.things.find({}).forEach(function(thing) {
var date1 = new Date(thing.lastModifiedAt);
var date2 = new Date('2016/04/12 16:24:18');
var dateDiff = date1.getTime() - date2.getTime();
printjson({_id:thing._id,lastModifiedAt:thing.lastModifiedAt,dateDiff:dateDiff});
});
This results in:
{
"_id" : ObjectId("570f1e528163383227ace761"),
"lastModifiedAt" : "2016/04/12 20:24:18",
"dateDiff" : 14400000
}
where dateDiff is milliseconds and 14400000 milliseconds = 4 hours.
If you provide more information on how you plan on making this call, and where the second date is coming from I would be happy to expand upon this answer.

HIBERNATE : Don't want to save milliseconds

I got a little problem and I didn't find a suitable solution on the net because my question is a bit tricky for search engine.
There's a lot of topics about hibernate saving milliseconds. But my problem is something else.
In fact, I got a database, which save my date like this :
2014-03-20 10:58:09
I used Hibernate to get back my date, and display it on a web page. But Hibernate retrieve more than that : it also retrieve a 0 milliseconds, like this :
2014-03-20 10:58:09.0
Many people seems to have problem with this, but in my case, I DON'T WANT this information, I want Hibernate to retrieve the date without this .0 !
Thanks for your help !
EDIT AND SOLUTION :
Ok so I made it by using a little hack.
In my specific object using by Hibernate, I had this method :
public Date getModificationDate() {
return modificationDate;
}
I just simply create an other method :
private static final SimpleDateFormat FMT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public String getModificationDateLabel() {
if (modificationDate != null) {
return FMT.format(modificationDate);
}
return null;
}
So, when I display in my webpage (I use Velocity Template), I just run through my list of object an display the label :
#foreach( $object in $objects)
$!{object.modificationDateLabel}
#end
The SimpleDateFormat allow me to remove the .0, and by creating a new method, I don't disturb the behavior of getting a Date with Hibernate.
Thanks for your time !
I don't see a problem with the date returned as "2014-03-20 10:58:09.0" is equal to "2014-03-20 10:58:09". Can you provide specific scenario where this can result in issue?
Or use SimpleDateFormat("yyyy-MM-dd HH:mm:ss") then parse your date in this format before using the date.

Assigning Google Calendar access to all users in a group (Java)

I can create a google calendar in my Java app.
I can assign it to a particular user in my Google domain:
AclRule rule = new AclRule();
Scope scope = new Scope();
scope.setType("user");
scope.setValue("user1#mydomain.com");
rule.setScope(scope);
rule.setRole("reader");
and the created calendar appears in user1's calendars.
Yet when I try to assign some other calendar to a group (which said user1 belongs to):
AclRule rule = new AclRule();
Scope scope = new Scope();
scope.setType("group");
scope.setValue("group1#mydomain.com");
rule.setScope(scope);
rule.setRole("reader");
user1 couldn't see the created calendar in his calendar list.
But the calendar is created successfully.
Is this correct functionality? Or should I just add Acl rule per group user?
Or I am doing something wrong and there is other way to assign some calendar to all the users in a group?
for future people running into problems: I had this issue and it's because the creator of the calendar wasn't in the group. Make sure the "sharer" is in the google group you are sharing your calendar with
When assigning Calendar access to a group, every group user gets an invitation to said Calendar.

save user data during a day (the same day -> many user data)

I have an application where the user enters data in edittext and presses the save button.
By pressing 'save' I save in a file the user data (in one column) and the current date (in the other column).
Then , I press another button and make the plot (using achartengine) date (x axis) data (y axis).
So, entering data during a day ,results in saving for example: "1" (user data) -> 20/4/2013 , "2" -> 20/4/2013 , "3" -> 20/4/2013.
And in plot I have 3 points in y axis (ok) and 3 points in x axis (not ok).
I want to have one point in x axis because the data where entered in the same day.
I save data :
public void savefunc(){
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
String formattedDate=thedate.format(d);
Log.d("tag","format"+formattedDate);
dates_Strings.add(formattedDate);
double thedata=Double.parseDouble(value.getText().toString().trim());
mydata.add(thedata);
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "MyFiles");
directory.mkdirs();
File file = new File(directory, filename);
FileOutputStream fos;
//saving them
try {
fos = new FileOutputStream(file);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i=0;i<mydata.size();i++){
bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
}
...
How can I save the user data during a day ?
Maybe some check here : Date d=new Date(); ? To check if it is the same day.
Or here : bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
But I can't figure.
For example I enter data " 1" , "2" ,"3" in date "20/4/2013".
This is what I get now using my code:
But i require graph like below: data entered on same day should be put together::
---------------UPDATE--------------------------------------------------
mRenderer.setXLabels(0);
for (int i=0;i<mydata.size();i++){
mRenderer.addXTextLabel(i,dates_Strings.get(i));
Date lastDate=null;
String lastdate="";
try{
// the initial date
Date initialDate=formatter.parse(dates_Strings.get(mydata.size()-1));
Calendar c = Calendar.getInstance();
c.setTime(initialDate);
c.add(Calendar.DATE, 1); // increase date by one
lastDate =c.getTime();
}catch ...
}
mRenderer.setXAxisMax(lastDate.getTime());
mRenderer.addXTextLabel(i,dates_Strings.get(i));
}
This is an AChartEngine issue indeed. The internal model used to be kept in ArrayLists and these issues didn't exist. At some point there was a community effort to make AChartEngine faster with a lot of data points. At that point, the model started to use a Map instead of an ArrayList. This implementation prevented having the same X value added several times. However, in order to fix this, I add a very small value to X, if it already exists. In your example, the first value is 20/04/2013 00:00:00.0, the second one is at 20/04/2013 00:00:00.001 and the third is 20/04/2013 00:00:00.002.
Now, the solution to your problem is to have a wider range on the X axis.
renderer.setXAxisMax(someDate.getTime());
where someDate can be something like 21/04/2013.
ok.
When you call new Date(), you also determine time of creation (default format is: January 1, 1970, 00:00:00 GMT). Because your points are created in different time but same date, your points are not aligned.
So you should do it like this:
Calendar thisDay = Calendar.getInstance();
thisDay.set(Calendar.HOUR, 0);
thisDay.set(Calendar.MINUTE, 0);
thisDay.set(Calendar.SECOND, 0);
Date d=thisDay.getTime();//this returns Date :) - it is funny but true
then you can use d as current date :).
Hope it is true and it helps,
Toni
There are a few possible solutions for this problem:
instead of a date, put the unix time of the date (long value) . in order to show it, you can convert the unix time to formatted date .
since excel can handle dates, edit the output file and use "=Date(year,month,day)" or "=DATEVALUE("2013/4/20")"
this is all because the problem isn't even related to android. it's about showing the data. the data is ok. it's just how you show it.
If I am not very much mistaken this is not a problem of saving or loading the data but simply of displaying the data. Your graph algorithm should recognize equal dates and do not make a new entry for it.
As it is, it seem like the date is treated as label, not as x-axis value, which would be reasonable because the date string is not numeric.
I suggest to check achartengine if there is a way to additional provide x-values and then let them only increase if the date string of the next entry is different of the previous entry.
You probably have to give a different model to achartengine.
I don't think it is a save problem because well the date stored is the right one, so any behavior there is mostly as expected.

Categories

Resources