I want to convert "Sat Oct 25 00:00:00 GMT+05:30 2008" into a date format "yyyy-mm-dd". How do i get this
Regards Anil
Vinod Awar
Ranch Hand
Joined: Nov 06, 2006
Posts: 125
posted
0
1) Use Date.parse() to parse this string . It will return a long value 2) Create a Date object with this long value 3) use java.text.SimpleDateFormat's format method to format it to the required date .
Vinod
The biggest bankruptcy is the loss of enthusiasm
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
posted
0
You don't need to do anything prior to use the date with SimpleDateFormat.
Do you need to format a date string or java.util.Date object?
For the latter, you can use simpleDateFormat.format(dateObject), simply. Otherwise, you have to parse your string to date, using simpleDateFormat.parse(stringDate), first prior to invoke simpleDateFormat.format(dateObject).
I hope that solves your problem exactly.
Anil Verghese
Ranch Hand
Joined: Oct 11, 2006
Posts: 155
posted
0
Hi,
Thanks guys for replying .. I have wriiten a java program to read datas from an excel sheet but this reads date as Sat Oct 25 00:00:00 GMT+05:30 2008.
I have tried to convert it using SimpleDateFormat... but it does'nt work. Can anyone shwo me how ?
Anil
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
0
I have tried to convert it using SimpleDateFormat... but it does'nt work.
Why don't you show us the code you have, and we may be able to suggest where it could be improved.
Anil Verghese
Ranch Hand
Joined: Oct 11, 2006
Posts: 155
posted
0
Hi,
This is my code , its actually for reading excel from java using poi...
import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import java.io.FileInputStream; import org.w3c.dom.ranges.Range; /** *Code for importing data from excel to MySql DataBase * Jakarta POI API * @author Anil&Kalis * @version 1.0 */ public class ReadXL { /** Location where the Excel has to be read from. Note the forward Slash */ public static String fileToBeRead="C:/Documents and Settings/Manager/Desktop/pigmy_details.xls"; public static void main(String argv[]){ cellCheck(); } public static void cellCheck() { try{
// Create a work book reference HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); // Refer to the sheet. Put the Name of the sheet to be referred from // Alternative you can also refer the sheet by index using getSheetAt(int index) HSSFSheet sheet = workbook.getSheet("Sheet"); //HSSFSheet sheets=workbook.getSheetAt(0); //Reading the TOP LEFT CELL try{ HSSFRow row = sheet.getRow(6);
/*// Create a cell ate index zero ( Top Left) HSSFCell cel1 = row.getCell((short)0); HSSFCell cel2 = row.getCell((short)1); HSSFCell cel3 = row.getCell((short)2); */ // Type the content //System.out.println("1." + cel1.getStringCellValue()+" 2."+ cel2.getStringCellValue()+" 3."+ cel3.getStringCellValue());
} catch(Exception e) { e.printStackTrace();
}
}catch(Exception e) { e.printStackTrace();
}
} private static boolean isDateFormatted1(HSSFCell cell) { switch (cell.getCellStyle().getDataFormat()) { case 14: case 165: case 167: case 168: case 169: case 170: case 171: case 172: case 173: case 174: return true; default: return true;//HSSFDateUtil.isCellDateFormatted(cell); } }
}
tell me if i can change this. I would like to make this as generic as possible.
Regards Anil
Vinod Awar
Ranch Hand
Joined: Nov 06, 2006
Posts: 125
posted
0
I have wriiten a java program to read datas from an excel sheet but this reads date as Sat Oct 25 00:00:00 GMT+05:30 2008.
Try by removing the "+05:30" from the string.
Vinod
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
posted
0
Pasting the complete lot doesn't really help most of the time. It would be better if you make it appropriate and paste the relevant snippet.
However, I hope you can use this, "EEE, MMM dd HH:mm:ss yyyy z", pattern to parse your string into date. And then there shouldn't be any problem in formating the date back into string in your desired format, I suppose.
Cheers.
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
BTW: If you do a several million these in a row, you might shave off a second or two by using substrings and concatenation. It's nowhere near as robust an OO technique but it can be quicker than some of the complex goings on in date formatters.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi