• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

conversion of month

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have started fresh with java and am writing servlets in jsp.
i am retreiving a date field from the database(ms-access)in mm/dd/yy (01/02/01) format but i have to display it on the front end as january,02,01.
on the jsp page ,i retreive the date in a string variable:
String dt=el.elementAt(i).toString();
even if i break this string for date,month,year..how do i convert month from 01 to january.
i would be really glad if u would guide me with this.
thank u in anticipation.
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SimpleDateFormat class in java.text package should be able to do what you need. Check out the API.

Bosun
 
k b
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at this also:--
import java.util.*;

public class TestConvert{
public static String getDate(String mmddyy)
{
// Assume your date is in mm/dd/yy format which you get from your vector
// Also assumed mm comes as 01, 02,...12 for jan thru Dec.
int mm = 0;
String smm = mmddyy.substring(0,2);
String sdd = mmddyy.substring(3,5);
String syy = mmddyy.substring(6,8);
try
{
mm = Integer.parseInt(smm);
}catch(Throwable t)
{
}
switch(mm)
{
case 1:
smm = "January";
break;
case 2:
smm = "February";
break;
case 3:
smm = "March";
break;
case 4:
smm = "April";
break;
case 5:
smm = "May";
break;
case 6:
smm = "June";
break;
case 7:
smm = "July";
break;
case 8:
smm = "August";
break;
case 9:
smm = "September";
break;
case 10:
smm = "October";
break;
case 11:
smm = "November";
break;
case 12:
smm = "December";
break;
default:
smm = "Invalid month";
}
return smm + "," + sdd + "," + syy;
}
public static void main(String[] args)
{
System.out.println(getDate(args[0]));
}
} // end of class

Note :-- compile and Test as
java TestConvert 01/25/01
Hope it helps.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try the follwing function for the conversion...(May be you'll have to modify this function.) This function is very robust and I have used it a thousand times.
public static String getDate(String scheduledDt)
{
String s;
s=scheduledDt;
s.trim();

System.out.println("Date : "+ s);
String yyyy = s.substring(0,4);
String mm = s.substring(5,7);
String dd = s.substring(8);
int int_year= Integer.parseInt(yyyy);
System.out.println("YYYY : "+ yyyy);
int int_day =Integer.parseInt(dd);
System.out.println("day : "+ dd);
int int_month=Integer.parseInt(mm);
System.out.println("month : "+ mm);
String mon = null;
//Date comaprison
if (int_month == 1)
{
mon = "jan";
}
else if (int_month == 2)
{
mon = "feb";
}
else if (int_month == 3)
{
mon = "mar";
}
else if (int_month == 4)
{
mon = "apr";
}
else if (int_month == 5)
{
mon = "may";
}
else if (int_month == 6)
{
mon = "jun";
}
else if (int_month == 7)
{
mon = "jul";
}
else if (int_month == 8)
{
mon = "aug";
}
else if (int_month == 9)
{
mon = "sep";
}
else if (int_month == 10)
{
mon = "oct";
}
else if (int_month == 11)
{
mon = "nov";
}
else
{
mon = "dec";
}
String formatedDate = dd + "-" + mon + "-" + yyyy;
System.out.println("Formatted Date : "+formatedDate );

return formatedDate;
}

Originally posted by paddy singh:
i have started fresh with java and am writing servlets in jsp.
i am retreiving a date field from the database(ms-access)in mm/dd/yy (01/02/01) format but i have to display it on the front end as january,02,01.
on the jsp page ,i retreive the date in a string variable:
String dt=el.elementAt(i).toString();
even if i break this string for date,month,year..how do i convert month from 01 to january.
i would be really glad if u would guide me with this.
thank u in anticipation.



------------------
Vikas Aggarwal
Technology Associate
http://www.vikinsa.com
 
Create symphonies in seed and soil. For this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic