Various date format conversions


import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GeneralUtil {

//Date Time Util
public static Timestamp getTimeStamp(String date) throws ParseException {
String DATE_FORMAT = "MM-dd-yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
Date today = dateFormat.parse(date);
Timestamp t1 = new Timestamp(today.getTime());
return t1;
}

public static String getCurrentDateTime(String format) {
DateFormat dateFormat = new SimpleDateFormat(format);
Date date = new Date();
return dateFormat.format(date);
}

public static String getDateMMDDYYY(Timestamp timestamp) {
String dateStr = "";

dateStr = timestamp.toString().substring(0, 10);
return dateStr;
}

public static String getDateFromDBStringMMDDYYYY(String theDate) {
String strOutDt = "";
try {
Date dtTmp = new SimpleDateFormat("yyyy-MM-dd").parse(theDate);
strOutDt = new SimpleDateFormat("MM-dd-yyyy").format(dtTmp);
} catch (ParseException pe) {
}
return strOutDt;
}

// public static void main(String[] args) throws ParseException {
//
public static String getDateFromTimeStamp(Timestamp t) {
String result = "";
try {
Timestamp dateTime = (Timestamp) t;

SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
result = dateFormat.format(dateTime);
return result;
} catch (Exception exception) {
}
return result;

}

public static String getDBDateFormat(String dateStr) {
String result = "";
try {
Date dtTmp = new SimpleDateFormat("MM-dd-yyyy").parse(dateStr);
result = new SimpleDateFormat("yyyy-MM-dd").format(dtTmp);
return result;
} catch (Exception exception) {
}
return result;

}

public static double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
//End Date Time Util

public static void main(String[] args) throws ParseException {
String str = "11-24-2010";

}
}

Date Format Example2


/*
Output:

on Tue Oct 01 00:00:00 PDT 2013 in Vancouver, B.C.
on Sun Mar 01 00:00:00 PST 1248 in Ottawa, ON
on Mon Jun 06 00:00:00 PST 1323 in Toronto, ON

*/

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatDemo2 {

public static void main(String[] a) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String input[] = { "2013-10-01 Vancouver, B.C.",
"1248-03-01 Ottawa, ON",
"1323-06-06 Toronto, ON" };
for (int i = 0; i < input.length; i++) {
ParsePosition pp = new ParsePosition(0);
Date d = formatter.parse(input[i], pp);
if (d == null) {
System.err.println("Invalid date in " + input[i]);
continue;
}
String location = input[i].substring(pp.getIndex());
System.out.println(" on " + d + " in " + location);

}
}
}

Date Format Example


import java.util.*;
import java.text.*;
class DateFormatDemo
{
public static void main(String A[])
{
//String dob="23/12/1998";
//String dob="DEC/23/1998";
String dob="1998-23-12";
//SimpleDateFormat sdf=new SimpleDateFormat("yyyy'/'dd'/'MM");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
// Date d=null;
try{

Date d=sdf.parse(dob);//01/31/98
System.out.println("Formated Date="+d);
System.out.println("Formated Date="+sdf.format(d));
}
catch(ParseException pe)
{
System.out.println("Parse Exception Is"+pe);
}

}
}