-
Java class StringFormat
I'm kind of a newie, I'm using Eclipse and I'm going through some examples where they use the class StringFormat (more exactly the method isValidDate ncluded in this class). But I can't find this class. I searched on the net for it and found out it may be in the package java.lang or java.text but I couldn't find in these packages. Does someone know what I have to do to be able to use this class (download it, install it) ?
Thanks a lot!
-
I'm a newbie too.
I'm not aware of any StringFormat class, but there's a format method in the String class which allows you to create formatted strings.
For example:
Code:
String myString = String.format("there are %d kinds of butterfly", 2145);
Is that the sort of thing you're wanting to do?
-
Not quite. I'd like to verify wether is a string represents a valid date by using the moethod isValidDate of the class StringFormat. But I can't use this method as I can't find the class.
-
This sorta works:
Code:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String date = "12/12/2121";
try
{
Date d = df.parse(date);
System.out.println(d);
}
catch(Exception e)
{
System.out.println("This is not a valid Date: " + date);
}
-
The only StringFormat class I find is StringFormat which has no isValidDate.
What example in eclipse are you talking about ... or could you qoute some text from there?
-
This example provides methods for validating the data.
The code is:
package com.ora.jsp.beans.userinfo;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import com.ora.jsp.util.*;
import com.ora.*;
import com.ora.jsp.beans.userinfo.*;
import com.ora.jsp.*;
/**
* This class contains information about a user. It's used to show
* how a bean can be used to validate user input and format output
* so it's suitable for HTML.
@author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
* @version 1.0
*/
public class UserInfoBean implements Serializable {
// Validation constants
private static String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
private static String[] SEX_LIST = {"male", "female"};
private static int MIN_LUCKY_NUMBER = 0;
private static int MAX_LUCKY_NUMBER = 100;
// Properties
private String birthDate;
private String birthDateInput;
private String emailAddr;
private String emailAddrInput;
private String[] interests;
private String luckyNumber;
private String luckyNumberInput;
private String sex;
private String sexInput;
private String userName;
private boolean isInitialized;
/**
* Returns the birthDate property value
*/
public String getBirthDate() {
return (birthDate == null ? "" : birthDate);
}
/**
* Returns the birthDate property value,
* with all HTML special characters converted
* to HTML character entities
*/
public String getBirthDateFormatted() {
return StringFormat.toHTMLString(getBirthDate());
}
/**
* Sets the birthDate property value, if it's
* valid
*/
public void setBirthDate(String birthDate) {
isInitialized = true;
birthDateInput = birthDate;
if (StringFomat.isValidDate(birthDate, DATE_FORMAT_PATTERN)) {
this.birthDate = birthDate;
}
......
The effect is that only validated values can be accessed though the getter methods
-
Perhaps one of those com.* packages that are being imported contains a StringFomat class that has an isValidDate method. You would have to check the API docs for them.
-
Probably the class is in the package written with red (com.ora.jsp.util.*;) I don't have this package (an error occures when compiling). Where could I find this package and how can I add it to my project?
-
Sorry I don't know. Perhaps you can find it wherever you got the rest of the com.* packages. Perhaps you don't really need that util if you can write your own data checker.