aspose file tools*
The moose likes Servlets and the fly likes Retrive Month and Year Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Retrive Month and Year" Watch "Retrive Month and Year" New topic
Author

Retrive Month and Year

Pranit Saha
Ranch Hand

Joined: Sep 09, 2001
Posts: 130
How can i retrieve month,day and year from a date.. is there any function in Date class.. pls help..
Pranit..
anand chawla
Ranch Hand

Joined: Oct 19, 2000
Posts: 91
you can use this
Date d1=new Date();
out.println(d1.getMonth());
out.println(d1.getDay());
out.println(d1.getYear());
probably this methods may give warning as they are deprecated...
else use...simpledateformat or Calendar..
check the jdk1.3 specification
Dharmesh Chheda
Ranch Hand

Joined: May 08, 2001
Posts: 204
hello Pranit
Well to extract Month Year and stuff do the following
Calendar cal=Calendar.getInstance();
out.println(cal.get(Calendar.MONTH));
out.println(cal.get(Calendar.YEAR));
out.println(cal.get(Calendar.DAY_OF_WEEK));
you can use all these to get the day month and stuff in case you want to display the dat in proper format then use SimpleDateFormat class .

------------------
IBM Certified WebSphere Application Server V3.5 Specialist


regards,<br />Dharmesh Chheda
Pranit Saha
Ranch Hand

Joined: Sep 09, 2001
Posts: 130
Thanks Dharmesh for ur reply..
The thing is the date is coming like 2001-10-31 00:00:00..
i want to display this like 31-15-2001..
How can i do this..
Pranit
Originally posted by Dharmesh Chheda:
hello Pranit
Well to extract Month Year and stuff do the following
Calendar cal=Calendar.getInstance();
out.println(cal.get(Calendar.MONTH));
out.println(cal.get(Calendar.YEAR));
out.println(cal.get(Calendar.DAY_OF_WEEK));
you can use all these to get the day month and stuff in case you want to display the dat in proper format then use SimpleDateFormat class .


namita pa
Ranch Hand

Joined: Sep 06, 2001
Posts: 56
hi
you can write a javascript function in the jsp and call it to format the date
String formatDate(String sDt)
{
sDt= sDt.substring(8,10)+"-"+sDt.substring(5,7)+"-"+sDt.substring(0,4);
return sDt;
}
where sDt is the date you get from the Calendar function
hope it will help

------------------
Dharmesh Chheda
Ranch Hand

Joined: May 08, 2001
Posts: 204
Hey Pranit
is that the actual way you are getting your date? if so
then either namita's way will help i am not sure cause its her idea i havent gone through it properly
or maybe you can use the StringTokenizer Class to get the date
eg.
If you are getting the date as 2001-12-31 00:00:00
String currentDate="2001-12-31 00:00:00";
String theDate=null;
String theTime=null;
StringTokenizer stz=new StringTokenizer(currentDate," ");
while(stz.hasMoreTokens())
{
theDate=stz.nextToken();
theTime=stz.nextToken();
}

----------------------
by this you will get
theDate="2001-12-31"
theTime="00:00:00";
----------------------

after this is done you can now seperate the year month and date using the StringTokenizer again ...
String theYear,theMonth,theDay;
StringTokenizer stz2=new StringTokenizer(theDate,"-");
while(stz2.hasMoreElements())
{
theYear=stz2.nextToken();
theMonth=stz.nextToken();
theDay=stz.nextToken();
}
out.println(theDay + "-" +theMonth+ "-" + theYear);

------------------
IBM Certified WebSphere Application Server V3.5 Specialist
[This message has been edited by Dharmesh Chheda (edited October 16, 2001).]
Pranit Saha
Ranch Hand

Joined: Sep 09, 2001
Posts: 130
Thanks a lot Dharmesh,
Ur way is correct but i was searching for any function like datepart() in databases.. or as vb is having some functions like day,month and year to retrieve the parts of the date.. Thanks anyway.. it's allright..
Pranit..
Originally posted by Dharmesh Chheda:
Hey Pranit
is that the actual way you are getting your date? if so
then either namita's way will help i am not sure cause its her idea i havent gone through it properly
or maybe you can use the StringTokenizer Class to get the date
eg.
If you are getting the date as 2001-12-31 00:00:00
String currentDate="2001-12-31 00:00:00";
String theDate=null;
String theTime=null;
StringTokenizer stz=new StringTokenizer(currentDate," ");
while(stz.hasMoreTokens())
{
theDate=stz.nextToken();
theTime=stz.nextToken();
}

Bal Sharma
Ranch Hand

Joined: Sep 19, 2001
Posts: 273
Hello:
Why do not use as below? It seems like VB to me.
Calendar calnedar = Calendar.getInstance();
int month = calnedar.get(Calendar.MONTH);
int year = calnedar.get(Calendar.YEAR);
int today = calnedar.get(Calendar.DAY_OF_MONTH);
Try it out it might come out as you wanted. BK
Originally posted by Pranit Saha:
Thanks a lot Dharmesh,
Ur way is correct but ...

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think the answer to your question might be here:
http://www.javaranch.com/ubb/Forum7/HTML/006264.html
 
Don't get me started about those stupid light bulbs.
 
subject: Retrive Month and Year