Converting date into day of year
I'm trying to write a program that prints the day of the year, given the date is in "month day year" form ie: 1 1 05, is day number 1 and 12 25 05 is day number 359.
the program should also check for a leap year.
I found this in a Google search and it's rediculously close to what i'm looking to achive, but instead of "today's date" i need it to read a day that was entered using Scanner or JOptionPane... and it would have to be in the above format when being entered.
import java.util.Calendar;
public class CalendarExample
public static void main(String[] args)
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
int dow = cal.get(Calendar.DAY_OF_WEEK);
int dom = cal.get(Calendar.DAY_OF_MONTH);
int doy = cal.get(Calendar.DAY_OF_YEAR);
System.out.println("Current Date: " + cal.getTime());
System.out.println("Day: " + day);
System.out.println("Month: " + month);
System.out.println("Year: " + year);
System.out.println("Day of Week: " + dow);
System.out.println("Day of Month: " + dom);
System.out.println("Day of Year: " + doy);
this if for a begining level java class, and I have no idea where to get started. Any help would be appreciated.
End result
import java.util.Scanner;
public class dayoftheYear
public static void main(String[] args)
//Declare variables
int day, month, year, doy=0, Jan=31, Feb=28, Febleap=29, Mar=31;
int Apr=30, May=31, Jun=30, Jul=31, Aug=31, Sep=30, Oct=31, Nov=30, Dec=31;
//Description
System.out.println("************************************************************");
System.out.println("This application will"
+ " use input for month and day "
+ "\nto determine what day of the year "
+ "it is. It will also check "
+ "\nto see if it's a leap year and adjust "
+ "\nthe day of the year accordingly");
System.out.println("************************************************************");
System.out.println();
//Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
//Input and print number.
System.out.print("Input month: ");
//Assign input
month = keyboard.nextInt();
//Input and print number.
System.out.print("Input day: ");
//Assign input
day = keyboard.nextInt();
//Input and print number.
System.out.print("Input year: ");
//Assign input
year = keyboard.nextInt();
//If leapyear, Feb has 29 days
if(isLeap(year)==1)
Feb =29;
//Determine day of year
switch (month)
case 1: doy=day; break;
case 2: doy=Jan+day; break;
case 3: doy=Jan+Feb+day; break;
case 4: doy=Jan+Feb+Mar+day; break;
case 5: doy=Jan+Feb+Mar+Apr+day; break;
case 6: doy=Jan+Feb+Mar+Apr+May+day; break;
case 7: doy=Jan+Feb+Mar+Apr+May+Jun+day; break;
case 8: doy=Jan+Feb+Mar+Apr+May+Jun+Jul+day; break;
case 9: doy=Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+day; break;
case 10: doy=Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+day; break;
case 11: doy=Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+day; break;
case 12: doy=Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sep+Oct+Nov+day; break; //hey! that looks pretty!
default: System.out.println("Invalid month.");break;
//Print day of year
System.out.println("Day of year: " +doy);
}//endmain
//beginmethod
public static int isLeap(int y)
//Assuming Y2K compliancy is not an issue, all dates will be based in the year 2000+
y += 2000;
//Check if year is a leap year
if(y%4==0&&y%100!=0)
return 1; //yes
else if(y%100==0&&y%400==0)
return 1; //yes
else
return 0; //no
}//endmethod
}
Similar Messages
-
HOW TO CONVERT DATE INTO NUMBERS
HI,
what the syntax for converting date into No. Ex from the first date of the month till current date how many days are there, this No of days needs to be divided by the net sale of the month.
i have searched in formulas fields but i am not able to find such option...
please help.
Thanking you,
UMAR NAYAB.Hi,
Stratos and Sastry
the formula given by you shows the variation for the current month of suppose 30 days
but if the month is of 31 days or 28 days (feb) how will the system will able to generate the report accordingly.
i want to make a report in which it should show the net sales by the current month and current date.
so the system should automated by the number of days in a month.
thanking you..
UMAR NAYAB. -
Convert date format from MONTH-YEAR to dd/mm/yyyy
Dear All
i want to get parameter in form of MONTH-YEAR AND WANT TO CONVERT DATE INTO 'THIS FORMAT dd/mm/yyyy' IT GIVES ME ERROR PLEASE GUIDE.
SELECT
TO_CHAR (PTP.END_DATE,'MONTH-YY') ,
to_date ( TO_CHAR (PTP.END_DATE ,'DD-MONTH-YYYY') ,'dd/mm/yyyy'),
--- i want to gent parameter in from of MONTH-YEAR AND WANT TO CONVERT DATE INTO 'THIS FORMAT dd/mm/yyyy' IT GIVES ME ERROR PLEASE GUIDE
to_date ( TO_CHAR (PTP.END_DATE ,'MONTH-YYYY') ,'dd/mm/yyyy'),
ADD_MONTHS((to_date ( TO_CHAR (PTP.END_DATE ,'MONTH-YYYY') ,'dd/mm/yyyy'),-1)
--- i want to gent parameter in from of MONTH-YEAR AND WANT TO CONVERT DATE INTO 'THIS FORMAT dd/mm/yyyy' IT GIVES ME ERROR PLEASE GUIDE
ptp.END_DATE PERRIODS_NAME
from per_time_periods ptp
Regards
Edited by: user10941925 on Nov 28, 2011 12:23 AMHello just check
SQL> select hiredate from emp;
HIREDATE
17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81
28-SEP-81
01-MAY-81
09-JUN-81
09-DEC-82
17-NOV-81
08-SEP-81
12-JAN-83
03-DEC-81
03-DEC-81
23-JAN-82
14 rows selected.
SQL> select to_char(hiredate,'month-year') from emp;
TO_CHAR(HIREDATE,'MONTH-YEAR')
december -nineteen eighty
february -nineteen eighty-one
february -nineteen eighty-one
april -nineteen eighty-one
september-nineteen eighty-one
may -nineteen eighty-one
june -nineteen eighty-one
december -nineteen eighty-two
november -nineteen eighty-one
september-nineteen eighty-one
january -nineteen eighty-three
december -nineteen eighty-one
december -nineteen eighty-one
january -nineteen eighty-two
14 rows selected.
SQL> select to_char(hiredate,'dd/mm/yyyy') from emp;
TO_CHAR(HI
17/12/1980
20/02/1981
22/02/1981
02/04/1981
28/09/1981
01/05/1981
09/06/1981
09/12/1982
17/11/1981
08/09/1981
12/01/1983
03/12/1981
03/12/1981
23/01/1982
14 rows selected.
SQL> -
Methods in bapi used to convert data into XML
Methods in bapi used to convert data into XML,
how to implement those also,
points will be rewarded
Thank you,
Regards,
Jagrut BharatKumar ShuklaHi
Check this
http://www.sap-img.com/abap/sample-xml-source-code-for-sap.htm
https://forums.sdn.sap.com/click.jspa?searchID=2889167&messageID=3406594
Reward points for useful Answers
Regards
Anji -
Convert number of dayes per year to data
Please i need help in Convert number of dayes to date
I have ID's like id
FT*09025*0000000001 date=20090125
FT*01171*0000000002 date=20100620
so should convert first five number after FT to date
example *09025*
09 map to 2009
025 number of dayes per year map to 25 jan
example *01171*
01 map to 2010
171 number of dayes per year map to 20 jun
(jan+feb+mar+apr+may)=(31+28+31+30+31)=151
171-151=20 for the next month
Please need help so will added filter to informatica map with date
Edited by: user8929623 on Jul 4, 2010 7:04 PMWell, I do not follow why when 09 maps into 2009, 01 maps into 2010, not into 2001. Assuming it should be 2001, use YYDDD format:
SQL> with t as (
2 select 'FT*09025*0000000001' id from dual union all
3 select 'FT*01171*0000000002' from dual
4 )
5 select to_date(substr(id,4,5),'yyddd') dt
6 from t
7 /
DT
20090125
20010620
SQL> SY. -
Convert data into Date Format imported from MS SQL Server.
I have imported Data from MS SQL Server. The "Date Column" received in number format like 41017.6361109954. How can i convert it into Date in Oracle SQL.
If i import same Data in Excel and change the Column Type to Date. It changes successfully. But in Oracle, I tried To_Date function with different parameters but it didn't work.
Edited by: XAVER on Apr 22, 2012 2:31 AMXAVER wrote:
The actual date for 41017.6361109954 is 22-Apr-2012 but its showing 20-APR-2082It looks like offset is January 1, 2000:
select timestamp '2000-01-01 00:00:00' + numtodsinterval(41017.6361109954,'day') from dual;
20-APR-12 03.15.59.990002560 PM
SQL> SY. -
Tool required to convert data into script
I have one staff table with following data
empno, name, dob
12345, kala, 25-aug-1960
23456, anugraha,05-mar-1991
34567,abiya,01-jul-1984
I want to send this data to another domain where the staff table exists with the same structure.
hence i need script file as follows
insert into staff values (12345, 'kala', '25-aug-1960');
insert into staff values (23456, 'anugraha','05-mar-1991');
insert into staff values (34567,'abiya','01-jul-1984');
i can run this script to load data into that new staff table under different domain.
Every day's transaction, i want to create this script file and store it in different domain.
Is there any facility to convert my entire data as above script file instead of writing procedures using cursors.In Oracle SQL Developer, you can right-click on a table and export the data as INSERT statements, among other formats.
Thanks,
- Scott -
http://sumnertechnologies.com/
http://spendolini.blogspot.com/ -
Converting Date Into Milliseconds?
I want to convert the following date and time into milliseconds: 4/21/2003 21.50. I know how to convert milliseconds into a date, but not the other way around. Could someone, please, help?
String etd = "4/21/2003 10:45";
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
df.setLenient(true);
try {
df.parse(etd);
catch(Exception e) {
e.printStackTrace();
I'm trying to use DateFormat to convert a date into ms. However, I'm getting a parsing error. Any suggestions? -
How to convert date into dd mon yyyy format
hi all,
i have a problem in date format i am using date like below .
<%java.util.Date date = new java.util.Date();%>
i am inserting date into a table and its storing like this
insert into tablename (d_date) values (date)
and its inserting date like below
Sun Oct 19 09:05:45 GMT+03:00 2003
i want to fetch date in dd mon yyyy format.
with this format i want to make a select query.i struck with the format conversion.
how to do this.
any comments please.
any helphi all,
i understand now where i am wrong.
the below code is not working why because in my server where i am executing code the regional setting month value is in arabic.
i executed the same code in a different server where date and time jones are english its working fine.
All the problem is in regional setting and not the jsp code.
<%
String whtEverDateFormatYouWAnt = "dd MMM yy";
String str = new SimpleDateFormat(whtEverDateFormatYouWAnt).format(new SimpleDateFormat("EEE MMM dd HH:mm:ss vvv yyyy").parse("Sun Oct 19 09:05:45 GMT+03:00 2003"));
out.print(str);
%>
Thanks a lot for the excellent solution.
Thanks again. -
How do i parse a date binding into day month year
<c:set var="test" scope="page" value="${bindings.StartDate1}" />
how do i pars off individual day month and year values so i can load them
into text boxes?use calendar insteed of date...
the difference is easy to find out:
long diffInMillis = calendar1.getTimeInMillis() - calendar2.getTimeInMillis()
hope this helps..
visit >> http://www.menzsoft.ch <<
rgds
chris -
Convert Date to Month and Year
Hi,
I am currently using Function Module CONVERSION_EXIT_LDATE_OUTPUT to convert a date example 20090901 to 01 September 2009.
Using z_date+3 then zdate becomes September 2009 which is what i require.
In the report when I actually sort by month it doesn't really work well example below.
September 2009
September 2010
October 2009
October 2010
November 2009
November 2010
It should really be like the below. I dont want to covert the date to 09.2009 i want it to be September 2009 hence i have taken this approach. Any help would be appreciated.
September 2009
October 2009
November 2009
September 2010
October 2010
November 2010
Regards
Adeelas you are storing it into one string, its just sorting on the basis of string comparison which starts from first later.
store them to two different fields.
like
data : BEGIN OF gt_dt OCCURS 0,
mnth type string,
year type n LENGTH 4,
end of gt_dt.
DATA : s1 TYPE string,
i1 type i.
*lets say here you get the values to this table like:
s1 = '01 September 2009'.
shift s1 by 3 PLACES left.
i1 = strlen( s1 ).
i1 = i1 - 4.
gt_dt-mnth = s1+0(i1).
shift s1 by i1 PLACES left.
gt_dt-year = s1.
APPEND gt_dt.
s1 = '01 September 2010'.
shift s1 by 3 PLACES left.
i1 = strlen( s1 ).
i1 = i1 - 4.
gt_dt-mnth = s1+0(i1).
shift s1 by i1 PLACES left.
gt_dt-year = s1.
APPEND gt_dt.
s1 = '01 October 2009'.
shift s1 by 3 PLACES left.
i1 = strlen( s1 ).
i1 = i1 - 4.
gt_dt-mnth = s1+0(i1).
shift s1 by i1 PLACES left.
gt_dt-year = s1.
APPEND gt_dt.
s1 = '01 October 2010'.
shift s1 by 3 PLACES left.
i1 = strlen( s1 ).
i1 = i1 - 4.
gt_dt-mnth = s1+0(i1).
shift s1 by i1 PLACES left.
gt_dt-year = s1.
APPEND gt_dt.
now you can sort as you wish. -
Hi
Does any one know how to get the month and date if I have the day of the year.
I did the opposite using cal.get(Calendar.DAY_OF_YEAR) from the Calendar class.
Thanks in advanceGregorianCalendar gc = new GregorianCalendar();
gc.set(gc.YEAR, someYear);
gc.set(gc.DAY_OF_YEAR, someDayCount);
int month = gc.get(gc.MONTH);
int day = gc.get(gc.DAY_OF_MONTH); -
Dear All,
I have date column.I have to display Month name and Year.
E.g
NOV 2011
DEC 2011
How will get this could you pls send me Syntex
ThanksHi Govind,
There are many ways to do,
1. you can use evaluate function to do that.
2. simple and better way is to overide the date formate of thet coloumn with the custom and then type MMM YYYY
Hope this helps you... -
Why dose Numbers 3.0 while opening csv-Files converts Dates into Integers?
I want to open a csv-file with a date.
The Date is in german notation.
28.10.2013 the value in the cell is 40113.
This problem occures only in Numbers 3.0
if I open the same file with Numbers 2.3 it is ok.
How can I change it.
The Systempreferences of the date and time handling is ok.2. Even though I have set the data type to text within EPMA, once I deploy the application and check it with the EAS console, the data type is NUMERIC - NOT TEXT anymore. This blows my mind. I don't know why this happens.
Its common ...As enter a text wavlue which has been entered in planning as text in number...Their are certain table's in planning which play role while fetching such values....
Cheers!
Sh!va -
Converting Date of Bith to Age
Is it possible to use a formula to convert a whole column of DOB's into ages.
ThanksTSCM88 wrote:
Thank you very much Barry, your post was very helpful. I took your advice and downloaded the user guides. My aim is to convert dates into years and months. I have tried to create a formula for this but it will not work, would very much appreciate any tips. I am unable to combine the DATEIF function with a month calculating function, i have been trying EDATE.
Depends what you mean by "a month calculating function" and "combine".
Did you check the DATEDIF entry in the F&F User Guide and try some of the calculation methods listed?
You can calculate the difference between two dates in years, months, weeks or days, or combinations of these.
This formula, for instance will give the age today in years and months for a birthdate placed in column B:
=DATEDIF(B,TODAY(),"Y")&"y, "&DATEDIF(B,TODAY(),"YM")&"m"
Regards,
Barry
Maybe you are looking for
-
How to add content source in office 365 ?
How to add content source in office 365 , where I can add find Manage Content Sources page to click on New Content Source LInk
-
Use java code to start ftp server in Windows
I am writing an application using java to start the FTP server on Windows system Could anybody give me an idea on how to start?
-
How to restore an ipad thats locked
Hi, i need some help unlocking and/or restoring my iPad. I just bought it today and set a passcode on it. I tried getting on it later and it kept telling me the passcode was incorrect. My iPad is now disabled and when i hook it up to my computer i
-
In the blue bar on the top of the popup it says: JavaScript Application In the box it says: Uninstall Set
-
HT1338 How to Reinstall ilife apps and ICal from Original disc...
My hard drive crashed. With my new hard drive, I somehow no longer have Calendar (iCal), Pages, Numbers or Keynote. I have my original discs from getting my computer July of 2011. How do I reinstall the apps I've lost? I don't want to mess up any oth