• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

date

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to know about Date Util.In fact I am trying to write a program in which the Date of Birth entered by user will be checked.If the date is a future date ....there will be error warrning "Invalid DOB".So what I need is to change the entered date to my format and compare it with the current date.
Please help me...I need some good source codes and examples.

I am a little urgent...Thank you.
 
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read this.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use java simpledateformat to convert the format of date to whatever format is required.

here is sample code.



import java.util.*;
import java.text.*;

/** class to hold Dynex specific timestamp format */
public class TimeStamp
{
/** XCBL FORMAT 20021213T00:00:00+08:00 */
private static final String xcblFormat = "yyyyMMdd'T'hh:mm:ss";
/** string to hold timestamp format */
private static final String format1 = "yyyyMMddhhmmss";
/** 16/09/2002 15:43:00 */
private static final String format2 = "dd/MM/yyyy hh:mm:ss";
/** 20020404T00:00:00+10:00 combine to make previous date */
private static final String format3 = "yyyyMMdd";
private static final String format4 = "hh:mm:ss";
private static final String format5 = "dd/MM/yyyy";
private static final String format6 = "yyyy-MMM-dd";
private static final String format7 = "yyyy-MM-dd hh:mm:ss";
private static final String mimeFormat = "EE','d MMM yyyy hh:mm:ss Z '('z')'";

private String format1Result = "";
private String format2Result = "";
private String format3Result = "";
private String format4Result = "";
private String format5Result = "";
private String format6Result = "";
private String format7Result = "";
private String mimeResult = "";

private Date DateFormat1 = null;
private Date DateFormat2 = null;
private Date DateFormat3 = null;
private Date DateFormat4 = null;
private Date DateFormat5 = null;
private Date DateFormat6 = null;
private Date DateFormat7 = null;
private Date DateFormatMime = null;

private Date now = null;

/** Get a string representaion of a date in format 1
* @return the date as a string in format 1
*/
public String getFormat1()
{
return format1Result;
}
/** Get a string representaion of a date in format 2
* @return the date as a string in format 2
*/
public String getFormat2()
{
return format2Result;
}
/** Get a string representaion of a date in format 3
* @return the date as a string in format 3
*/
public String getFormat3()
{
return format3Result;
}
/** Get a string representaion of a date in format 4
* @return the date as a string in format 4
*/
public String getFormat4()
{
return format4Result;
}
/** Get a string representaion of a date in format 5
* @return the date as a string in format 5
*/
public String getFormat5()
{
return format5Result;
}
/** Get a string representaion of a date in format 6
* @return the date as a string in format 6
*/
public String getFormat6()
{
return format6Result;
}

/** Get a string representaion of a date in format 7
* @return the date as a string in format 7
*/
public String getFormat7()
{
return format7Result;
}
/** Get a string representaion of a date in XCBL format
* @return the date as a string in XCBL format
*/
public String getXCBLFormat()
{
return format3Result + "T" + format4Result;
}
/** Get a string representaion of a date in MIME format
* @return the date as a string in MIME format
*/
public String getMimeFormat()
{
return mimeResult;
}
/** Get a Date in format 1
* @return the date in format 1
*/
public Date getDateFormat1()
{
return DateFormat1;
}
/** Get a Date in format 2
* @return the date in format 2
*/
public Date getDateFormat2()
{
return DateFormat2;
}
/** Get a Date in format 3
* @return the date in format 3
*/
public Date getDateFormat3()
{
return DateFormat3;
}
/** Get a Date in format 4
* @return the date in format 4
*/
public Date getDateFormat4()
{
return DateFormat4;
}
/** Get a Date in format 5
* @return the date in format 5
*/
public Date getDateFormat5()
{
return DateFormat5;
}
/** Get a Date in format 6
* @return the date in format 6
*/
public Date getDateFormat6()
{
return DateFormat6;
}
/** Get a Date in format 7
* @return the date in format 7
*/
public Date getDateFormat7()
{
return DateFormat7;
}
/** Get a Date
* @return the date
*/
public Date getDate()
{
if(now == null)
{
generateTimeStamp();
}
return now;
}

/**
* parse a string into date format 2
* @param val
* @return
*/
public Date parseDateFormat2(String val) throws Exception
{
return new SimpleDateFormat(format2).parse(val);
}

/** convert a date (22/12/2003) to and XCBL date ()
* @param normalDate the date to convert
* @return an XCBL format string of the date
*/
public String convertToXCBL(Date normalDate)
{
String ret = "";

try
{
SimpleDateFormat formatterX = new SimpleDateFormat(xcblFormat);
ret = formatterX.format(normalDate);
}
catch(Exception e)
{
}

return ret;
}

public String convertDate(String origFormat, String requiredFormat, String dateVal) throws Exception
{
SimpleDateFormat origFormatter = new SimpleDateFormat(origFormat);

Date retDate = origFormatter.parse(dateVal);

SimpleDateFormat resFormatter = new SimpleDateFormat(requiredFormat);

return resFormatter.format(retDate);

}

/** generate a new current timestamp */
public void generateTimeStamp()
{
SimpleDateFormat formatter1 = new SimpleDateFormat(format1);
SimpleDateFormat formatter2 = new SimpleDateFormat(format2);
SimpleDateFormat formatter3 = new SimpleDateFormat(format3);
SimpleDateFormat formatter4 = new SimpleDateFormat(format4);
SimpleDateFormat formatter5 = new SimpleDateFormat(format5);
SimpleDateFormat formatter6 = new SimpleDateFormat(format6);
SimpleDateFormat formatter7 = new SimpleDateFormat(format7);
SimpleDateFormat formatterMime = new SimpleDateFormat(mimeFormat);

now = new Date();

format1Result = formatter1.format(now);
format2Result = formatter2.format(now);
format3Result = formatter3.format(now);
format4Result = formatter4.format(now);
format5Result = formatter5.format(now);
format6Result = formatter6.format(now);
format7Result = formatter7.format(now);
mimeResult = formatterMime.format(now);

}

/** Set the date
* @param val the date to set
*/
public void setDate(Date val)
{
SimpleDateFormat formatter1 = new SimpleDateFormat(format1);
SimpleDateFormat formatter2 = new SimpleDateFormat(format2);
SimpleDateFormat formatter3 = new SimpleDateFormat(format3);
SimpleDateFormat formatter4 = new SimpleDateFormat(format4);
SimpleDateFormat formatter5 = new SimpleDateFormat(format5);
SimpleDateFormat formatter6 = new SimpleDateFormat(format6);
SimpleDateFormat formatter7 = new SimpleDateFormat(format7);
SimpleDateFormat formatterMime = new SimpleDateFormat(mimeFormat);

format1Result = formatter1.format(val);
format2Result = formatter2.format(val);
format3Result = formatter3.format(val);
format4Result = formatter4.format(val);
format5Result = formatter5.format(val);
format6Result = formatter6.format(val);
format7Result = formatter7.format(val);
mimeResult = formatterMime.format(val);
}

/** Generate a date in a specific format
* @param val the string value of the date
* @param format the formt to generate
*/
public void generateDate(String val, int format)
{
try
{
if(format == 1)
{
SimpleDateFormat formatter1 = new SimpleDateFormat(format1);
DateFormat1 = formatter1.parse(val);
}

if(format == 2)
{
SimpleDateFormat formatter2 = new SimpleDateFormat(format2);
DateFormat2 = formatter2.parse(val);
}

if(format == 3)
{
SimpleDateFormat formatter3 = new SimpleDateFormat(format3);
DateFormat3 = formatter3.parse(val);
}

if(format == 4)
{
SimpleDateFormat formatter4 = new SimpleDateFormat(format4);
DateFormat4 = formatter4.parse(val);
}

if(format == 5)
{
SimpleDateFormat formatter5 = new SimpleDateFormat(format5);
DateFormat5 = formatter5.parse(val);
}

if(format == 6)
{
SimpleDateFormat formatter6 = new SimpleDateFormat(format6);
DateFormat6 = formatter6.parse(val);
}

if(format == 7)
{
SimpleDateFormat formatter7 = new SimpleDateFormat(format7);
DateFormat7 = formatter7.parse(val);
}

if(format == 8)
{
SimpleDateFormat formatterMime = new SimpleDateFormat(mimeFormat);
DateFormatMime = formatterMime.parse(val);
}
}
catch(ParseException pe)
{
pe.printStackTrace();
}
}

/** Main entry point for program
* @param args the arguements
*/
public static void main(String[] args)
{
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic