File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i format a string to date

 
Anil Verghese
Ranch Hand
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to do anything prior to use the date with SimpleDateFormat.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and more specifically, the SimpleDateFormat.parse method can parse dates from strings. The "Date(String)" constructor is deprecated.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic