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

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.

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:

How to extract the latest date from an object (which is a list of dates, Java)

I have an object list_of_dates, of the following form :
[2018-06-18T00:00Z, 2018-06-23T00:00Z, 2018-06-02T00:00Z,...,2018-06-21T00:00Z]
I would like to extract the latest date. I can not do this with the help of:
//here, I assume object is transformed into list and then sorted.
Collections.sort(Arrays.asList(list_of_dates));
return list_of_dates[list_of_dates.length-1];
Could you suggest why I get an error?
Your code should work fine, here is another solution where you can find the max or the min
ZonedDateTime[] list_of_dates = new ZonedDateTime[n];
//...
ZonedDateTime maxDate = Stream.of(list_of_dates).max(ZonedDateTime::compareTo).get();

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

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.

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.

Categories

Resources