I am developing a java system that will keep track of a certain company's fixed assets, calculate the depreciating value using the straight line accounting method so the depreciating value will always be constant. after calculating all this it should divide the figure by 12 to get a monthly depreciating value and then alert the user (finance) of the depreciating after every financial year. how can I make the java based app get the time from the local machine to trigger these alerts.
First set the financial year of your app to, for instance, (31/03/2015) and then compare with the current date from your local machine. Check out the code for clarity:
if(getCurrentDate().equals((31/03/2015))||getCurrentDate().after((31/03/2015))){
//then do something here
}
And here is the method to get current date:
public static java.util.Date getCurrentDate() {
int month = GregorianCalendar.getInstance().get(Calendar.MONTH);
int year = GregorianCalendar.getInstance().get(Calendar.YEAR);
int day = GregorianCalendar.getInstance().get(Calendar.DATE);
Calendar calender = Calendar.getInstance();
calender.set(year, month, day);
return calender.getTime();
}
Related
I had try to get day different between two specific date using below code but the same date passed in return a different value for android below and above android 6.0
here is my function to get the day count:
Calendar cal = Calendar.getInstance();
Calendar base_cal = Calendar.getInstance();
baseDate = chineseDateFormat.parse("1900-1-3");
base_cal.setTime(baseDate);
int offset = (int) ((cal.getTimeInMillis() - base_cal.getTimeInMillis()) / 86400000L);
I realized that the cal.getTimeInMillis() can return the same value for different os version but base_cal.getTimeInMillis() will return different value for different android os version and it always different by one day
Your "baseDate" is the result of parsing a date string. I don't read Chinese, so I can't tell what the format is, but I suspect that the problem is that your parsed date has a different time of day from the date returned by getTimeInMillis(), and the result is a rounding error.
I further suspect that what you expect is the difference between the two dates, without respect to time of day.
The easiest way to do this correctly and repeatably is to use the Joda date library. There's a port specifically for Android at https://github.com/dlew/joda-time-android
You can see an example at Number of days between two dates in Joda-Time
I cannot for the life of me get my head around how I should write this program.
The program
The task is to write a Java program that calculates how many days old a person is. The program will ask a user for birth data that the user enters in the form YYYYMMDD (as an integer, not a string). Subsequently, from the current date taken from your computer, see below) how many days old the person is, and this result is printed.
Programs dialog when you run the program looks like this:
When were you born? 19930102
Then you are today 7,707 days old.
Note - the program shall take into account any leap days.
Limitation
The calculation of how many days old a person is to be done by your own code, not the practice in any existing date class.
Today's date
To get the current date from the computer can GregorianCalendar class is used. When you create an object of that class is read computer time. You can then get the year, month and day of the object with the method get. How it works:
GregorianCalendar greg = new GregorianCalendar ();
int year = greg.get (Calendar.YEAR);
int month = greg.get (Calendar.MONTH);
int day = greg.get (Calendar.DAY_OF_MONTH);
You will get a result for year, month and day. Note, however, that month is returned as a number 0-11, ie 0 for January, 1 for February, etc.
I won't write any code, but here is an algorithm that should do this. I am not saying that this is efficient! But since the calculation is limited (no human being is 100000+.. years old) it can be used:
1st prompt the user to type his birthday in YYYYMMDD format as a STRING
2nd use substring to get YYYY MM DD (this should be allowed, I guess)
3rd convert YYYY MM and DD into integers
4th use the code you provided to get todays date
Let your algorithm assume a year has 365 days
5th calculate year (greg.get (Calendar.YEAR) MINUS YYYY
6th todays month - birth month: If negative then safe -MONTHS+DAYSINTHEMONTH in a variable ELSE +MONTHS*DAYSINTHEMONTH
7th calculate todays day - birth day: safe this in a value and add this to your final variable
8th now calculate ((todays year - birth year)*365)+(value of step 6, can be pos. or negative)+(value of step 7, can be pos. or negative)
Print out your answer to the console!
What you have to think of is how to get the exact amount of days in a month (which can be hard coded of course, since you are not allowed to use any classes at all).
I have this code in my project
Calendar subval = Calendar.getInstance();
final int WOY= subval.WEEK_OF_YEAR;
and when I check it for value of WOY it outputs 3 now it is currently Feb 25 2013 and I know the week number is not three. I am storing this value to help set automatic refresh times so I am able to force a refresh to make sure the device has the most current data. In between refresh periods some crucial data is stored locally. Now I need a reliable fixed time slot and I chose once a week basically if the WEEK OF YEAR is not the same as the stored value for WEEK OF YEAR set data to be refreshed at next opportunity and then store current WEEK OF YEAR on device. I started coding this within 1 week so I have not transitioned to the new week so I am not sure if it working correctly but the value of three scares me.
WEEK_OF_YEAR is a flag used to the get() method. It's value never changes.
You use it like this:
Calendar subval = Calendar.getInstance();
final int WOY= subval.get(Calendar.WEEK_OF_YEAR);
Calendar.WEEK_OF_YEAR is a constant to be used to specify which field to return from your Calendar instance. Instead of assigning the value to the fixed value, you need to call Calendar#get:
int WOY = subval.get(Calendar.WEEK_OF_YEAR);
You need to get it like this
Calendar subval = Calendar.getInstance();
int year=subval.get(Calender.Calendar.WEEK_OF_YEAR);
Calendar.WEEK_OF_YEAR is a static constant.
You need to call calendar.get(Calendar.WEEK_OF_YEAR)
Subclasses define WEEK_OF_YEAR for days before the first week of year according to the java oracle documentation
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html
see: week_of_year
So I'm working on an application that needs to display a value, a specific string of text once per day. I have all my strings in an array and now I just need a way to increase the index once per day. The kicker is that if the user downloads the application later in the year I need to have all the other days accounted for. So basically the user will see the same tip as a person who downloaded the app on the first day. Any suggestions?
Would using the Calendar class be my best bet? I just don't want to set an individual switch and case for every day of the year.
Not sure if I understand completely the question. I think you will need to have the same tip shown for everyone in each day, right?
If so, you can use Calendar.DAY_OF YEAR :
Calendar cal = Calendar.getInstance();
int index=cal.get(Calendar.DAY_OF_YEAR);
if I understand you correctly (Bob downloaded the app a hundred days ago should see the same tip as Alice who downloaded the application today), you can use Calendar's DAY_OF_YEAR value to display the same date
Calendar ca1 = Calendar.getInstance(); //get today's date
int DAY_OF_YEAR=ca1.get(Calendar.DAY_OF_YEAR) -1; //DAY_OF_YEAR starts at one
//avoid IndexOutOfBoundsExceptions
String tip = tiparray[DAY_OF_YEAR % tiparray.length];
Hey everyone in my web application i am generating a verification code that is to be entered by user for account activation. I want that the generated verification code should be valid for specific time period say 12 hrs or 24 hrs. I am storing details of date and time when code is generated but how can i check for validity means suppose user entered code how can i check that he has entered code in validation period or not? How to compare date and time together? Thanks
I wouldn't think of it as a date and time as such. I'd think of it as an instant in time. When you generate the verification code, work out the instant in time "24 hours from now", and store that. Then just check "the current instant" when you validate the verification code.
You could use System.currentTimeMillis() to get the instant as a long value in milliseconds. (It's since the Unix epoch, but that's actually irrelevant here.) In fact, I'd suggest you create a kind of Clock interface which can be used to determine the current instant, with an implementation using System.currentTimeMillis() - or the Joda Time equivalent. That way you can unit test your code more easily.
So take the current number of milliseconds, add your appropriate duration to it (again, in milliseconds), and that's your expiry instant.
Use a database such as my sql to store verification code. have a field called createdTime. When user tries to verify, subtract createdTime from now() to get less than 24.
RESPONSE: date subtraction
select something from someTable where createdTime >= date_sub(now(), interval 24 hour)
http://docs.oracle.com/javase/7/docs/api/java/util/Date.html
after() and before() methods do ecactly what you want.
The Java Date class has before() and after() functions for comparing dates.
You can use before or after of Calender object
For example:
// initialize ur date here
Date issueded = null;
Calendar issuedDate = Calendar.getInstance();
issuedDate.setTime(issueded);
Calendar expiredDate = Calendar.getInstance();
// minus 12 H
expiredDate.add(Calendar.HOUR, -12);
if(expiredDate.before(issuedDate)) {
// do ur thing
} else {
// expired
}