I am trying to make a billing menu for a software. What i am trying to do is, I want to assign a bill id to each bill using the date, say 24/11/2022 will be converted as 24112022. This will be followed by a count as: 24112022-01. I want this counter to reset whenever the date changes. I can't figure it out. Any solutions?
Here is the code I am using to get the date values and use them as my bill id
LocalDate date=LocalDate.now();
int d=date.getDayOfMonth();
int m=date.getMonthValue();
int y=date.getYear();
billId=String.valueOf(d)+String.valueOf(m)+String.valueOf(y)+counter++;
"I want this counter to reset whenever the date changes."
Think analytically (re-framing the question for this context):
a) You want a counter to reset when the date changes
b) When the date changes, you want the counter reset
c) Upon a data change, reset the counter.
So, you may want to occasionally check the time and see if it has changed (having a stored value to compare against) or overwrite an existing method that changes the day so that it would also reset the value.
Related
Trying to move from Calendar to the new Java 8 time on Android. Is there a way to indicate that a time or date field is not supported? I can use the 'Truncate' method that will set all time fields of a shorter duration to zero, so a time stamp like 2020-09-30T10:37:15.345-04:00 can be truncated say at the minutes level. But that will leave 2020-09-30T10:00:00.00-04:00.
However, what I want to indicate is that the clock does not have minutes or less precision so that when one tries to read the minutes or seconds, there will be some indication that there are no such fields or that they are unknown. Zero is a valid value.
Right now in the Calendar case I have to add numerous methods to a class to indicate that. For example, I made a class called TimeStruct which wraps a Calendar. If I take a time stamp like 2020-10-01T04:55 it does not have minutes. So to keep that information I have a variable 'isSecondsSet' and set it to false. I create the Calendar from the elements I DO have. But as soon as I call something like Calendar.getTimeInMillis() the seconds and milliseconds fields get set to 0 and are indicated as set. So my additional variables let me know that there was no seconds field.
I was hoping that the new classes would no longer require me to keep my own indicators and I would also be able to parse something like 2020-10-01T04:55. I could not, but I could parse a full time stamp. So if I do that and truncate, can I indicate that the truncated fields are not supported? That way I wont use a value of 0 in the seconds.
I would like to have you help of private Duration duration = Duration.ofMinutes(0); it is set for 0 minute. I have this line of code in as a class variable. It supposed to be a counter. It counts back from the time set. What i have done is, I have created a GUI and created a texfield where user can enter an integer and that integer should be passed to the Duration.ofMintues(). I have tried many different ways without succeed, now i would like to ask your help to countinue on with it.
reference: csibman
Java: How to convert a string (HH:MM:SS) to a duration?
I'm trying to setup my addToDay function. I'm currently stuck on how to proceed with this or even write it correctly. The function itself will take a variable that ranges from -100 to 100. So you would basically add that variable to the current and if it was below the 0 then subtract a month or if it was above the months max day then add a month. Which i have that function setup so all i would have to do is call addToMonth with the correct amount. My problem lies within the amount of days each month has. For example, October has 31 days while November has 30. I have a function that will return the number of days in the current set month so i can call that to get how many max days should be in the current month. I'm thinking maybe a while loop would work but i just wanted to get anyone's thoughts on the best way to set it up.
I have 3 private ints: month, day, year. These are what need to be changed. I have both addTo functions for month and year setup already.
Here are some other functions i have created that can be used in this:
1. addToMonth(int delta) - changes the current month depending on the given parameter
2. getDaysInMonth() - will return the days in a month depending on the month itself
3. validateDay() - Will return true or false if the days fall outside the wanted requirements.
I don't want to use the calendar utility
I also don't want to use any other utilities. Just the base code with Junit for testing
Joda's plusDays() function and Java 8 LocalDate already has the logic that you are trying to achieve
Alright so i ended up just copying my original addToMonth function and modifying it abit to fit with days. So far it works but i do think it'll fail in the cases of different amounth of days not lining up.
Thank you very much in advance for reading.
I'm programming a web application in Struts.
I'm using a JQuery datepicker 1.7 as a calendar on the sidebar on the user's view. (It's the best option I could find)
I would like to highlight in the calendar a group of days starting from initial date (startDate) until the end (endDate) of several reminders I have in an array. This way, the user will be able to see on the calendar of the application, all the available days he has left in order to take action for each of his reminders.
I already have my array of reminders which I can access from the view. I was able to implement it properly thanks to guidance from a great fellow around here.
This is my javascript function for the datepicker:
$(function(){
$('#datepicker').datepicker({
flat: true,
numberOfMonths: [1,1],
dateFormat: 'dd/mm/yy',
beforeShowDay: highlightDays
});
There is another function, namely, highlightDays which is the one that is causing me trouble:
It's parameter is the array "reminders" such that reminders[i].start is the startDate, and reminders[i].end is the endDate of reminder i, with i = number of reminders in the array.
function highlightDays(reminders) {
for (var i = 0; i < reminders.length; i++) {
/** Below:
*If startDate is smaller than endDate,
*then highlight the days inbetween.
*/
if (new Date(reminders[i].start).toString() <=
new Date(reminders[i].end).toString() ){
return [true, 'ui-state-highlight'];
}
} //Otherwise do not highlight
return [true, ''];
}
The problem is the calendar won't even show up when I open the application and log in. And the Apache Log or output do not show any errors. I'd like to know, where do you think I might be wrong? I will keep investigating, but I'd really appreciate your input!
P.D: I can access the elements of the array of reminders (endDate, startDate) and print these dates on screen, so that means the reminders array is not empty.
Thank you once again for taking the time to read.
From http://jqueryui.com/demos/datepicker/#event-beforeShowDay
The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name(s) or '' for the default presentation, and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.
Your callback function is not accepting a single date as a parameter, it's taking an array of date ranges.
You might want to figure out how to call the highlightdays function inside the loop of remdinder values.
With this C code:
int a = time(NULL);
_daylight = 0;
_timezone = 0;
int b = time(NULL);
assert(a != b);
"a" and "b" will have different values (and not just because they are called a few milliseconds apart). The difference will be whatever the offset of your PC's timezone is from UTC time. Also, changing the _daylight and _timezone values effect pretty much every other function I might use in my C app -- I assume because they all respect that value.
Is there anything like that in Java, or specifically for Java on Android OS? I tried TimeZone.setDefault(), but that didn't change the value that System.currentTimeMillis() returned, so I assume it isn't going to have a "global" effect like the C variables.
I understand that System.currentTimeMillis() is different than time(), in that it "always" returns the number of millis since now and epoch, and the time() function allows you to get "false" (fudged) values that are adjusted according to these global variables you can set.
Just trying to emulate a legacy C app on Android OS. It clears those _timezone and _daylight values which pretty much means it ignores any timezones. So if a user running the app on the west coast enters a time of 3pm, and then they change their timezone settings, or a user on the est coast views that item, it will still show as 3pm.
I know I can use the Calendar object and other methods to make sure I do the proper conversions, but I'd rather just have an easy "I don't care about timezones" settings like I did in the C app and then truely not have to worry about them.
Edit: I would still like to hear what other options I have, but for now I came up with this Java code that I'll do my best to always use for any code that needs to mimic the C app:
// IMPORTANT: Use this function everywhere a Calendar object is needed, instead of calling
// Calendar.getInstance() directly. This returns the correct kludged time that matches
// what our PC application uses (_daylight=0, _timezone=0, time(NULL) in C)
public static Calendar GetCalendarInstance()
{
// Get the current UTC time
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
// Offset it by the system time zone offset.
// This mimics what the C time(NULL) function does when you set _timezone=0 and _daylight=0
cal.add(Calendar.MILLISECOND, TimeZone.getDefault().getOffset(cal.getTimeInMillis()));
return(cal);
}
Also, I did already find one place in my Android app that I need the real, not adjusted, system time (when using AlarmManager to schedule a PendingIntent). So I guess "global" could be dangerous either way. I still think 95% of my code will be using the version that mimics the C app though, so if possible I'd like to default to that and then only have to do special handling for the other few places.