need assistant, i need to change the default format (2017-01-18) for XMLGregorianCalendar to example "20170118" , i have tried a lot of examples through here but its not helping
private static SimpleDateFormat formats = new SimpleDateFormat("yyyyMMdd");
public static XMLGregorianCalendar unmarshal(String value) {
try {
return toXMLGregorianCalendar(formats.parse(value));
} catch ( ParseException e ) {
e.printStackTrace();
return null;
}
}
I'm afraid you cannot do that. If you take a look into the class XMLGregorianCalendar, you'll find that the toString() method just call the toXMLFormat() and the toXMLFormat() doesn't provide any possibility for format customization.
public String toXMLFormat() {
QName typekind = getXMLSchemaType();
String formatString = null;
// Fix 4971612: invalid SCCS macro substitution in data string
// no %{alpha}% to avoid SCCS macro substitution
if (typekind == DatatypeConstants.DATETIME) {
formatString = "%Y-%M-%DT%h:%m:%s" + "%z";
} else if (typekind == DatatypeConstants.DATE) {
formatString = "%Y-%M-%D" + "%z";
} else if (typekind == DatatypeConstants.TIME) {
formatString = "%h:%m:%s" + "%z";
} else if (typekind == DatatypeConstants.GMONTH) {
formatString = "--%M" + "%z";
} else if (typekind == DatatypeConstants.GDAY) {
formatString = "---%D" + "%z";
} else if (typekind == DatatypeConstants.GYEAR) {
formatString = "%Y" + "%z";
} else if (typekind == DatatypeConstants.GYEARMONTH) {
formatString = "%Y-%M" + "%z";
} else if (typekind == DatatypeConstants.GMONTHDAY) {
formatString = "--%M-%D" + "%z";
}
return format(formatString);
}
Is there any work-around solution?
Well, if you just want to get a string of type yyyyMMdd from a XMLGregorianCalendar object, you can do:
XMLGregorianCalendar c = YourCalendarHelper.unmarshal("2017-01-18");
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String str = format.format(c.toGregorianCalendar().getTime());
System.out.println(str); // 20170118
By the way, if an exception raised during the conversion, DO NOT catch it unless you know you're 100% sure how to handle it. Throw it through the method declaration, so that the caller this method is aware of the potential failure.
Related
I have below Java code to convert string format to Timestamp object
public class TestUtil{
Object result;
Public Object convertFormat(String format, String value, String type){
String format = "yyyyMMddHHmmss";
String value = "20050225144824";
SimpleDateFormat dformat = new SimpleDateFormat(format);
java.util.Date date = dformat.parse(value);
result = new Timestamp(date.getTime);
System.out.println("Result::"+ result);
}
}
Expected outcome:
I was expecting the outcome should be like below
20050225144824
Actual outcome:
2005-02-25 14:48:24.0
Could anyone tell me what I am missing here? To get "20050225144824" this result
The below code runs fine for me.
Adding few print statements to explain the different behaviors.
import java.util.Date;
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
public class Main
{
public static void main(String[] args) {
String myFormat = "yyyyMMddHHmmss";
String value = "20050225144824";
try {
SimpleDateFormat dformat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = dformat.parse(value);
Timestamp ts = new Timestamp(date.getTime());
Object result = new Timestamp(date.getTime());
System.out.println("Timestamp Format with yyyyMMddHHmmss : " +dformat.format(ts));
System.out.println("Object Format with yyyyMMddHHmmss : " +result);
System.out.println("Object Format with yyyyMMddHHmmss : " +dformat.format(result));
} catch(Exception e) {
e.printStackTrace();
}
}
}
Here is the output of the different behaviors :
Timestamp Format with yyyyMMddHHmmss : 20050225144824
Object Format with yyyyMMddHHmmss : 2005-02-25 14:48:24.0
Object Format with yyyyMMddHHmmss : 20050225144824
If you expect Timestamp to return your custom output then you need to override the default Timestamp library.
Here I create CustomTimestamp.java to extend Timestamp and override its toString() method. I modified the changes according to your requirement.
public class CustomTimestamp extends Timestamp {
private int nanos;
public CustomTimestamp(long time) {
super(time);
}
#Override
public String toString () {
int year = super.getYear() + 1900;
int month = super.getMonth() + 1;
int day = super.getDate();
int hour = super.getHours();
int minute = super.getMinutes();
int second = super.getSeconds();
String yearString;
String monthString;
String dayString;
String hourString;
String minuteString;
String secondString;
String nanosString;
String zeros = "000000000";
String yearZeros = "0000";
StringBuffer timestampBuf;
if (year < 1000) {
// Add leading zeros
yearString = "" + year;
yearString = yearZeros.substring(0, (4-yearString.length())) +
yearString;
} else {
yearString = "" + year;
}
if (month < 10) {
monthString = "0" + month;
} else {
monthString = Integer.toString(month);
}
if (day < 10) {
dayString = "0" + day;
} else {
dayString = Integer.toString(day);
}
if (hour < 10) {
hourString = "0" + hour;
} else {
hourString = Integer.toString(hour);
}
if (minute < 10) {
minuteString = "0" + minute;
} else {
minuteString = Integer.toString(minute);
}
if (second < 10) {
secondString = "0" + second;
} else {
secondString = Integer.toString(second);
}
if (nanos == 0) {
nanosString = "";
} else {
nanosString = Integer.toString(nanos);
// Add leading zeros
nanosString = zeros.substring(0, (9-nanosString.length())) +
nanosString;
// Truncate trailing zeros
char[] nanosChar = new char[nanosString.length()];
nanosString.getChars(0, nanosString.length(), nanosChar, 0);
int truncIndex = 8;
while (nanosChar[truncIndex] == '0') {
truncIndex--;
}
nanosString = new String(nanosChar, 0, truncIndex + 1);
}
// do a string buffer here instead.
timestampBuf = new StringBuffer(20+nanosString.length());
timestampBuf.append(yearString);
timestampBuf.append(monthString);
timestampBuf.append(dayString);
timestampBuf.append(hourString);
timestampBuf.append(minuteString);
timestampBuf.append(secondString);
timestampBuf.append(nanosString);
return (timestampBuf.toString());
}
}
Your main class should use CustomTimestamp to get the output
try {
String format = "yyyyMMddHHmmss";
String value = "20050225144824";
SimpleDateFormat dformat = new SimpleDateFormat(format);
java.util.Date date;
date = dformat.parse(value);
Timestamp result = new CustomTimestamp(date.getTime());
System.out.println("Result::" + result);
} catch (ParseException e) {
e.printStackTrace();
}
I am fetching data from file using filestream and importing this data into oracle tables. I have column 'FT__FIRST' which is Date data type in oracle table and where i only need date values to be inserted and ignore other values. From file the date is coming in format 'YYYYMMDD'. In future if there is other values coming from file rather than Date datatype for this column and if it tries to insert into oracle table then the java code might throw an error as literal string does not match.
So to avoid this issue i want to modify my java code such that it can take only insert date format value and ignore other values. Currently i am handling only specific string from file which i know and ignoring it as they are not date format..
Java uses classes DateTimeformatter but dont know how to use it in my code..
private String createUpdateTableSql(String line, String tableName, String dateFormat, List<ColumnData> columnData) {
List<String> data = Splitter.on("|").trimResults().splitToList(line);
String ftFirst = "";
String tr = "";
String pds = "";
for (int i = 0; i < columnData.size(); i++) {
if(columnData.get(i) == null || "N.A.".equalsIgnoreCase(data.get(i)) || "N.A".equalsIgnoreCase(data.get(i)) || "UNKNOWN".equalsIgnoreCase(data.get(i))) {
continue;
}
if ("FT_FIRST".equalsIgnoreCase(columnData.get(i).getName().trim())) {
ftFirst = data.get(i);
}
if ("TR".equalsIgnoreCase(columnData.get(i).getName().trim())) {
tr = data.get(i);
}
if ("P_S_SOURCE".equalsIgnoreCase(columnData.get(i).getName().trim())) {
pds = data.get(i);
}
}
return "UPDATE " + tableName + " " +
"SET FT_FIRST=to_date('" + ftFirst + "','YYYYMMDD')" +
" WHERE TR='" + ticker +
"' AND P_S_SOURCE='" + pds + "'";
}
When you read data from file, you could parse the date field to date object like below:
// NEW CODE
private Date getDateValue(String sDate, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return
sdf.parse(format);
} catch (ParseException ignored) {
// TODO: Log this exception return null;
}
}
As you can see, you are parsing a string to date with an expected format yyyyMMdd (YYYYMMDD on oracle)
If parsing fails (value field does not containts the expected format) you can add the logic as you want on catch clausule or ignore error and let value as null.
Your code will be like this:
private String createUpdateTableSql(String line, String tableName, String dateFormat, List<ColumnData> columnData) {
List<String> data = Splitter.on("|").trimResults().splitToList(line);
String futNoticeFirst = "";
String ticker = "";
String pds = "";
for (int i = 0; i < columnData.size(); i++) {
if (columnData.get(i) == null || "N.A.".equalsIgnoreCase(data.get(i)) || "N.A".equalsIgnoreCase(data.get(i)) || "UNKNOWN".equalsIgnoreCase(data.get(i))) {
continue;
}
if ("FUT_NOTICE_FIRST".equalsIgnoreCase(columnData.get(i).getName().trim())) {
futNoticeFirst = getDateValue(data.get(i), 'yyyyMMdd');
}
if ("TICKER".equalsIgnoreCase(columnData.get(i).getName().trim())) {
ticker = data.get(i);
}
if ("PARSEKYABLE_DES_SOURCE".equalsIgnoreCase(columnData.get(i).getName().trim())) {
pds = data.get(i);
}
}
return "UPDATE " + tableName + " " +
"SET FUT_NOTICE_FIRST= " + futNoticeFirst +
" WHERE TICKER='" + ticker +
"' AND PARSEKYABLE_DES_SOURCE='" + pds + "'";
}
// NEW CODE
private Date getDateValue(String sDate, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
return
sdf.parse(format);
} catch (ParseException ignored) {
// TODO: Log this exception
return null;
}
}
I am using Youtube data api v3 to get video information like title, views count and duration.The duration value is new to me as it's an ISO8601 date which I need to convert to a readable format like hh:mm:ss. Duration can have the following different values:
PT1S --> 00:01
PT1M --> 01:00
PT1H --> 01:00:00
PT1M1S --> 01:01
PT1H1S --> 01:00:01
PT1H1M1S --> 01:01:01
I could use Joda Time library to parse the value and calculate the duration in seconds but the library is of 500kb in size which will increase the size of my application that I don't want.
look at this code :
private static HashMap<String, String> regexMap = new HashMap<>();
private static String regex2two = "(?<=[^\\d])(\\d)(?=[^\\d])";
private static String two = "0$1";
public static void main(String[] args) {
regexMap.put("PT(\\d\\d)S", "00:$1");
regexMap.put("PT(\\d\\d)M", "$1:00");
regexMap.put("PT(\\d\\d)H", "$1:00:00");
regexMap.put("PT(\\d\\d)M(\\d\\d)S", "$1:$2");
regexMap.put("PT(\\d\\d)H(\\d\\d)S", "$1:00:$2");
regexMap.put("PT(\\d\\d)H(\\d\\d)M", "$1:$2:00");
regexMap.put("PT(\\d\\d)H(\\d\\d)M(\\d\\d)S", "$1:$2:$3");
String[] dates = { "PT1S", "PT1M", "PT1H", "PT1M1S", "PT1H1S", "PT1H1M", "PT1H1M1S", "PT10H1M13S", "PT10H1S", "PT1M11S" };
for (String date : dates) {
String d = date.replaceAll(regex2two, two);
String regex = getRegex(d);
if (regex == null) {
System.out.println(d + ": invalid");
continue;
}
String newDate = d.replaceAll(regex, regexMap.get(regex));
System.out.println(date + " : " +newDate);
}
}
private static String getRegex(String date) {
for (String r : regexMap.keySet())
if (Pattern.matches(r, date))
return r;
return null;
}
The regex2two has been used to add a leading zero0 to 1-digit numbers. you can try this demo.
In the regexMap I'v stored all 7 cases and appropriate regex-replace.
I did by myself
Let's try
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.
public class YouTubeDurationUtils {
/**
*
* #param duration
* #return "01:02:30"
*/
public static String convertYouTubeDuration(String duration) {
String youtubeDuration = duration; //"PT1H2M30S"; // "PT1M13S";
Calendar c = new GregorianCalendar();
try {
DateFormat df = new SimpleDateFormat("'PT'mm'M'ss'S'");
Date d = df.parse(youtubeDuration);
c.setTime(d);
} catch (ParseException e) {
try {
DateFormat df = new SimpleDateFormat("'PT'hh'H'mm'M'ss'S'");
Date d = df.parse(youtubeDuration);
c.setTime(d);
} catch (ParseException e1) {
try {
DateFormat df = new SimpleDateFormat("'PT'ss'S'");
Date d = df.parse(youtubeDuration);
c.setTime(d);
} catch (ParseException e2) {
}
}
}
c.setTimeZone(TimeZone.getDefault());
String time = "";
if ( c.get(Calendar.HOUR) > 0 ) {
if ( String.valueOf(c.get(Calendar.HOUR)).length() == 1 ) {
time += "0" + c.get(Calendar.HOUR);
}
else {
time += c.get(Calendar.HOUR);
}
time += ":";
}
// test minute
if ( String.valueOf(c.get(Calendar.MINUTE)).length() == 1 ) {
time += "0" + c.get(Calendar.MINUTE);
}
else {
time += c.get(Calendar.MINUTE);
}
time += ":";
// test second
if ( String.valueOf(c.get(Calendar.SECOND)).length() == 1 ) {
time += "0" + c.get(Calendar.SECOND);
}
else {
time += c.get(Calendar.SECOND);
}
return time ;
}
}
Had to deal with this problem as well. I had to convert the length to milliseconds, but once you get the secs/mins/hours variables populated you can convert to any format you want:
// Test Value
$vidLength = 'PT1H23M45S';
$secs = '';
$mins = '';
$hours = '';
$inspecting = '';
for($i=(strlen($vidLength)-1); $i>0; $i--){
if(is_numeric($vidLength[$i])){
if($inspecting == 'S'){
$secs = $vidLength[$i].$secs;
}
else if($inspecting == 'M'){
$mins = $vidLength[$i].$mins;
}
else if($inspecting == 'H'){
$hours = $vidLength[$i].$hours;
}
}
else {
$inspecting = $vidLength[$i];
}
}
$lengthInMS = 1000*(($hours*60*60) + ($mins*60) + $secs);
I needed a array of all these converted duration. So I wrote the below as a workaround and also java.time.duration was not working for me, don't know why.
String[] D_uration = new String[10];
while(iteratorSearchResults.hasNext()){String Apiduration1=Apiduration.replace("PT","");
if(Apiduration.indexOf("H")>=0){
String Apiduration2=Apiduration1.replace("H",":");
if(Apiduration.indexOf("M")>=0){
String Apiduration3=Apiduration2.replace("M",":");
if(Apiduration.indexOf("S")>=0){
D_uration[i]=Apiduration3.replace("S","");
}
else{
String Apiduration4=Apiduration2.replace("M",":00");
D_uration[i]=Apiduration4;
}
}
else{
String Apiduration4=Apiduration2.replace(":",":00:");
if(Apiduration.indexOf("S")>=0){
D_uration[i]=Apiduration4.replace("S","");
}
else{
String Apiduration3=Apiduration4.replace(":00:",":00:00");
D_uration[i]=Apiduration3;
}
}
}
else{
if(Apiduration.indexOf("M")>=0){
String Apiduration2=Apiduration1.replace("M",":");
if(Apiduration.indexOf("S")>=0){
D_uration[i]=Apiduration2.replace("S","");
}
else{
String Apiduration4=Apiduration2.replace(":",":00");
D_uration[i]=Apiduration4;
}
}
else{
D_uration[i]=Apiduration1.replace("S","");
}
}
"Apiduration" is returned by the Youtube data Api in ISO8601 format.
Made some edits now i think it should work fine.
Which exception can i use to check if the input has the right number of "/"
The input should be like DD/MM/YYYY
try{
String str = text.getText();
StringTokenizer st = new StringTokenizer(str);
String DD = st.nextToken("/");
String MM = st.nextToken("/");
String YYYY = st.nextToken();
}
catch( ???){
}
You will find it in the javadoc of nextToken.
It says it will throw a NoSuchElementException when there is no more token.
That said you should better not use the try/catch but test it using the hasMoreTokens method.
You can use Custom Exception for this but for that you need to declare method which validate your date (No of slashes).
Try Like this
public class Demo
{
public static void main (String[] args) {
try {
new MyClass().metToValidate("01/12/2014");
} catch (A e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class A extends Exception{}
class MyClass{
public void metToValidate(String dateText) throws A{
if( dateText.charAt(2) == '/'&& dateText.charAt(5) == '/' )
System.out.println("DATE IS OK");
else
throw new A();
}
}
The exception throws by nextToken is NoSuchElementException.
String str = "12/21223";
int counter = 0;
for( int i=0; i<str.length(); i++ ) {
if( str.charAt(i) == '/' ) {
counter++;
}
}
if(counter == 3){
StringTokenizer st = new StringTokenizer(str);
String DD = st.nextToken("/");
String MM = st.nextToken("/");
String YYYY = st.nextToken();
System.out.println(str);
}else{
System.out.println("Exception");
}
my application takes in a string like this "2002-10-15 10:55:01.000000". I need to validate that the string is a valid for a db2 timestamp.
How can I do this?
EDIT: This mostly works
public static boolean isTimeStampValid(String inputString) {
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
try {
format.parse(inputString);
return true;
} catch (ParseException e) {
return false;
}
}
The problem is that if I pass it a bad format for milliseconds like "2011-05-02 10:10:01.0av" this will pass validation. I am assuming that since the first millisecond character is valid then it just truncates the rest of the string.
I'm not exactly sure about the format but you you can play around it and can try something like this
public static bool isTimeStampValid(String inputString)
{
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
try{
format.parse(inputString);
return true;
}
catch(ParseException e)
{
return false;
}
}
EDIT: if you want to validate for numbers after successful parsing, you could do
format.parse(inputString);
Pattern p = Pattern.compile("^\\d{4}[-]?\\d{1,2}[-]?\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}[.]?\\d{1,6}$");
return p.matcher(inputString).matches();
instead of
format.parse(inputString);
return true;
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
I believe the format would be "yyyy-MM-dd HH:mm:ss.SSSSSS"
Call parse(String) and catch ParseException indicating it is invalid.
/**
* This method validates the given time stamp in String format
* #param timestamp
* #return
*/
public static boolean isTimeStampValid(String timestamp) {
//(Considering that formal will be yyyy-MM-dd HH:mm:ss.SSSSSS )
//Tokenize string and separate date and time
boolean time = false;
try {
//Tokenize string and separate date and time
StringTokenizer st = new StringTokenizer(timestamp, " ");
if (st.countTokens() != 2) {
return false;
}
String[] dateAndTime = new String[2];
int i = 0;
while (st.hasMoreTokens()) {
dateAndTime[i] = st.nextToken();
i++;
}
String timeToken = dateAndTime[1];
StringTokenizer timeTokens = new StringTokenizer(timeToken, ":");
if (timeTokens.countTokens() != 3) {
return false;
}
String[] timeAt = new String[4];
int j = 0;
while (timeTokens.hasMoreTokens()) {
timeAt[j] = timeTokens.nextToken();
j++;
}
try {
int HH = Integer.valueOf(timeAt[0].toString());
int mm = Integer.valueOf(timeAt[1].toString());
float ss = Float.valueOf(timeAt[2].toString());
if (HH < 60 && HH >= 0 && mm < 60 && mm >= 0 && ss < 60 && ss >= 0) {
time = true;
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
//Got Date
String dateToken = dateAndTime[0];//st.nextToken();
//Tokenize separated date and separate year-month-day
StringTokenizer dateTokens = new StringTokenizer(dateToken, "-");
if (dateTokens.countTokens() != 3) {
return false;
}
String[] tokenAt = new String[3];
//This will give token string array with year month and day value.
int k = 0;
while (dateTokens.hasMoreTokens()) {
tokenAt[k] = dateTokens.nextToken();
k++;
}
//Now try to create new date with got value of date
int dayInt = Integer.parseInt(tokenAt[2]);
int monthInt = Integer.parseInt(tokenAt[1]);
int yearInt = Integer.parseInt(tokenAt[0]);
Calendar cal = new GregorianCalendar();
cal.setLenient(false);
cal.set(yearInt, monthInt - 1, dayInt);
cal.getTime();//If not able to create date it will throw error
} catch (Exception e) {
e.printStackTrace();
return false;
}
//Here we ll check for correct format is provided else it ll return false
try {
Pattern p = Pattern.compile("^\\d{4}[-]?\\d{1,2}[-]?\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}[.]?\\d{1,6}$");
if (p.matcher(timestamp).matches()) {
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
//Cross checking with simple date format to get correct time stamp only
SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
try {
format.parse(timestamp);
//return true;
if (time) {
return true;
} else {
return false;
}
} catch (ParseException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
If you're already connected to the database, you can execute a query that attempts to cast the input string as a timestamp, and check for a failure message (in this case, SQLSTATE 22007).
VALUES CAST( ? AS TIMESTAMP )
The above query will fully validate the input string while consuming hardly any resources on the database server. If the string is invalid for any reason, your database client will encounter an exception.