• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrive Month and Year

 
Pranit Saha
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pranit Saha
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer to your question might be here:
http://www.javaranch.com/ubb/Forum7/HTML/006264.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic