wood burning stoves 2.0*
The moose likes Java in General and the fly likes How do i format a string to date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do i format a string to date" Watch "How do i format a string to date" New topic
Author

How do i format a string to date

Anil Verghese
Ranch Hand

Joined: Oct 11, 2006
Posts: 155
Hi,

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
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
You don't need to do anything prior to use the date with SimpleDateFormat.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Indeed, steps 1 and 2 that Vinod mentions are not necessary. Just use SimpleDateFormat to parse your String into a Date.

Note: Don't confuse the terms: you parse a String into a Date, and you format a Date into a String.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42263
    
  64
... and more specifically, the SimpleDateFormat.parse method can parse dates from strings. The "Date(String)" constructor is deprecated.


Ping & DNS - my free Android networking tools app
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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
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: 42263
    
  64
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
Hi,

This is my code , its actually for reading excel from java using poi...



package migration;



import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.SimpleFormatter;

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);

HSSFRow row1=null;
System.out.println("ROWS--"+sheet.getPhysicalNumberOfRows());
Iterator rows=sheet.rowIterator();
workbook.getNumberOfSheets();

while(rows.hasNext())
row1=(HSSFRow)rows.next();
// System.out.println("ROWS ::"+row1.getRowNum());
Iterator cells=row1.cellIterator();
while(cells.hasNext())
{
HSSFCell cell=(HSSFCell)cells.next();
//System.out.println("CELL ::"+cell.getCellNum());
// System.out.println("CELLVALUE ::"+cell.getStringCellValue());
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC :
if (isDateFormatted1(cell) ){

System.out.println("Date : " +cell.getDateCellValue());
// Sat Oct 25 00:00:00 GMT+05:30 2008
String formatDate=cell.getDateCellValue().toString();

}
else
System.out.println("Numeric : " + cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING :
System.out.println("String : " + cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK :
System.out.println("Blank : " + cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN :
System.out.println("Boolean : " + cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR :
System.out.println("Error : " + cell.getErrorCellValue());
break;
// case HSSFCell.CELL_TYPE_FORMULA :
// System.out.println("Formula : " + cell.getCellFormula());
// break;
default :
System.out.println("String : " + cell.getStringCellValue());
break;
}

}

/*// 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
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
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i format a string to date