[The function has been developed as a routine in the Talend.]
package routines; import java.text.ParseException; public class DateConv { public static String oConv(int secToAdd, String format) throws ParseException { final String date = "31/12/1967";//31 Dec 1967 java.util.Date returnDate = null; String returnString = null; java.text.DateFormat sdf = new java.text.SimpleDateFormat("dd/MM/yyyy"); java.util.Date parsedDate = sdf.parse(date); java.util.Calendar now = java.util.Calendar.getInstance(); now.setTime(parsedDate); if (format.equals("MT")) { // 02:46 sdf = new java.text.SimpleDateFormat("HH:mm"); now.add(java.util.Calendar.SECOND, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate); } else if (format.equals("MTS")) { // 02:46:40 sdf = new java.text.SimpleDateFormat("HH:mm:ss"); now.add(java.util.Calendar.SECOND, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate); } else if (format.equals("MTHS")) { // 02:46:40am sdf = new java.text.SimpleDateFormat("HH:mm:ssa"); now.add(java.util.Calendar.SECOND, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate).toLowerCase(); } else if (format.equals("MTH")) { // 02:46am sdf = new java.text.SimpleDateFormat("HH:mma"); now.add(java.util.Calendar.SECOND, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate).toLowerCase(); } else if (format.equals("MT.")) { // 02.46 sdf = new java.text.SimpleDateFormat("HH.mm"); now.add(java.util.Calendar.SECOND, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate); } else if (format.equals("D2")) { // 3 Feb 93 sdf = new java.text.SimpleDateFormat("d MMM yy"); now.add(java.util.Calendar.DAY_OF_MONTH, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate); } else if (format.equals("D/E")) { // 3/2/1993 sdf = new java.text.SimpleDateFormat("d/M/yyyy"); now.add(java.util.Calendar.DAY_OF_MONTH, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate); } else if (format.equals("DI")) { // 3/2/1993 sdf = new java.text.SimpleDateFormat("d/M/yyyy"); now.add(java.util.Calendar.DAY_OF_MONTH, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate); } else if (format.equals("D2-")) { // 2-3-93 sdf = new java.text.SimpleDateFormat("M-d-yy"); now.add(java.util.Calendar.DAY_OF_MONTH, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate); } else if (format.equals("D")) { // 31 Dec 1967 sdf = new java.text.SimpleDateFormat("d MMM yyyy"); now.add(java.util.Calendar.DAY_OF_MONTH, secToAdd); returnDate = now.getTime(); returnString = sdf.format(returnDate); } return returnString; } public static String oConv(String input, String format) throws ParseException { String returnString = null; java.text.DateFormat sdf = null; if (input.equalsIgnoreCase("@DATE") && format.equals("DMD[2,2]")) { //DMD[2,2] sdf = new java.text.SimpleDateFormat("MMdd"); returnString = sdf.format(new java.util.Date()); } else if (input.equalsIgnoreCase("@DATE") && format.equals("DYMD[4,2,2]")) { //DYMD[4,2,2] sdf = new java.text.SimpleDateFormat("yyyyMMdd"); returnString = sdf.format(new java.util.Date()); } return returnString; } }eg:
DateConv.oConv(10000, "MTHS"); // 02:46:40am
DateConv.oConv(9166, "D2"); // 3 Feb 93
0 Comments