wood burning stoves 2.0*
The moose likes Servlets and the fly likes conversion of month Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "conversion of month" Watch "conversion of month" New topic
Author

conversion of month

paddy singh
Greenhorn

Joined: Jun 11, 2001
Posts: 1
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.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
The SimpleDateFormat class in java.text package should be able to do what you need. Check out the API.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
k b
Greenhorn

Joined: Feb 01, 2001
Posts: 22
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.
vikinsa
Ranch Hand

Joined: Apr 18, 2001
Posts: 46
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


Vikas Aggarwal<BR>Technology Associate<BR>http://www.vikinsa.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conversion of month
 
Similar Threads
Date parse
Convert String to Date
Java Calendar Dates
Java Calendar Dates
About date format conversion