Saturday, January 28, 2017
Saturday, January 21, 2017
Saturday, January 14, 2017
Sunday, January 8, 2017
DateUtility class for converting String into Date and Vice Versa using Java
In this post, we will see how to convert String into Date and vice versa.
DateUtility.java
DateUtility.java
package com.ranga.utility; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Objects; import java.util.TimeZone; /* * Utility class for converting string into date. * @author Ranga Reddy * @version 1.0 * @since 08/Jan/2017 */ public class DateUtility { /** * Used to get the date in Date format. * @param dateInString - date in string format * @param format - format to get the date. * @return date parsed from the string. * @throws ParseException */ public static Date parseDate(String dateInString, String format) throws ParseException { Objects.requireNonNull(dateInString, "Date string value must not be null."); Objects.requireNonNull(format, "Format must not be null."); DateFormat dateFormat = new SimpleDateFormat(format); return dateFormat.parse(dateInString); } /** * Used to get the date in String format. * @param date - the date * @param pattern - the pattern * @return the date in String. */ public static String format(Date date, String pattern) { return format(date, pattern, null); } /** * Used to get the date in String format. * @param date - the date * @param pattern - the pattern * @param timeZone - the timeZone * @return */ public static String format(Date date, String pattern, TimeZone timeZone) { Objects.requireNonNull(date, "Date must not be null."); Objects.requireNonNull(pattern, "Pattern must not be null."); DateFormat formatter = new SimpleDateFormat(pattern); if (timeZone != null) { formatter.setTimeZone(timeZone); } return formatter.format(date); } /** * Used to convert the date in one format to another format. * @param fromDate - the from date * @param fromPattern - the from pattern * @param toPattern - the to pattern * @return formated date * @throws ParseException */ public static String convertDateFromOneFormatToAnother(String fromDate, String fromPattern, String toPattern) throws ParseException { Objects.requireNonNull(fromDate, "From Date must not be null."); Objects.requireNonNull(fromPattern, "From Pattern must not be null."); Objects.requireNonNull(toPattern, "To Pattern must not be null."); DateFormat fromDateFormat = new SimpleDateFormat(fromPattern); Date sourceDate = fromDateFormat.parse(fromDate); DateFormat toDateFormat = new SimpleDateFormat(toPattern); return toDateFormat.format(sourceDate); } /** * Used to validate the date. * * @param date - date in string. * @param format - format to check the date * @return true if date is valid otherwise return false. */ public static boolean isValidDate(String date, String pattern) { Objects.requireNonNull(date, "Date must not be null."); Objects.requireNonNull(pattern, "Pattern must not be null."); DateFormat dateFormat = new SimpleDateFormat(pattern); try { dateFormat.parse(date); return true; } catch (Exception e) { System.err.println("Encountered exception while validating the date. " + e); return false; } } }DateUtilityTest.java
package com.ranga.utility; import java.text.ParseException; import java.util.Date; /** * Used to test the DateUtility functionalities. * * @author Ranga Reddy * @version 1.0 * @since 08/Jan/2017 */ public class DateUtilityTest { public static void main(String[] args) throws ParseException { Date date = new Date(); String formatedDate = DateUtility.format(date, "yyyy-MMM-dd HH:mm:ss a"); System.out.println("Formatted date: "+formatedDate); String parseDateString = "01-Jul-1988"; Date convertedDate = DateUtility.parseDate(parseDateString, "dd-MMM-yyyy"); System.out.println("Converted date: "+convertedDate); boolean isValidDate = DateUtility.isValidDate(formatedDate, "yyyy-MMM-dd"); System.out.println("IsValid date: "+isValidDate); } }Output:
Formatted date: 2017-Jan-08 19:13:32 PM Converted date: Fri Jul 01 00:00:00 IST 1988 IsValid date: trueHappy Learning ...!
PropertyUtility class for reading properties using Java
In this post, we will see how to get the properties from multiple property file.
There are 3 ways to get the properties from property file:
- Using Class path
- Using Relative path
- Using Absolute path
1. Using Class Path: In order to get properties from class path, we need to use getResourceAsStream().
Example:
InputStream inputStream = PropertyUtility.class.getResourceAsStream(propertyFileName); properties.load(inputStream); inputStream.close();
2. Using Relative & Absolute Path: In order to get properties from relative/absolute path we need to use FileInputStream().
File file = new File(propertyFileName); InputStream inputStream = = new FileInputStream(file); properties.load(inputStream); inputStream.close();
package com.ranga.utility; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * Utility class for getting the properties * * @author Ranga Reddy * @since 01-Jan-2017 * @version 1.0 */ public class PropertyUtility { /** * Getting the properties from multiple property files. * * @param propertyFileNames * - property file names * @return - the properties * @throws IOException */ public static Properties getProperties(String... propertyFileNames) throws IOException { Properties properties = null; if (propertyFileNames != null) { properties = new Properties(); for (String propertyFileName : propertyFileNames) { File file = new File(propertyFileName); InputStream inputStream = null; if (file.exists()) { System.out.println("File <" + propertyFileName + "> found in the FileSystem."); inputStream = new FileInputStream(file); } else { inputStream = PropertyUtility.class.getResourceAsStream(propertyFileName); if (inputStream == null) { throw new FileNotFoundException("File <" + propertyFileName + "> is not found."); } System.out.println("File <" + propertyFileName + "> found in the classpath."); } properties.load(inputStream); inputStream.close(); } } return properties; } }Testing the Above code:
package com.ranga.utility; import java.io.IOException; import java.util.Properties; /** * Test Utility class for testing the properties * @author Ranga Reddy * @since 01-Jan-2017 * @version 1.0 */ public class PropertyUtilityTest { public static void main(String[] args) throws IOException { // loading the properties from class path file Properties classPathProperties = PropertyUtility.getProperties("/test.properties"); System.out.println("Class Path Properties: " + classPathProperties + "\n"); // loading the properties from relative path. Properties relativePathProperties = PropertyUtility.getProperties("test2.properties"); System.out.println("Relative Path Properties: " + relativePathProperties + "\n"); // loading the properties from absolute file path. Properties absolutePathProperties = PropertyUtility.getProperties( "C:\\Users\\RangaReddy\\Ranga_Utilities\\test2.properties"); System.out.println("Absolute Path Properties: " + absolutePathProperties + "\n"); // loading the properties from multiple files Properties multipleFileProperties = PropertyUtility.getProperties( "/test.properties", "test2.properties"); System.out.println("Multiple file Properties: " + multipleFileProperties + "\n"); // loading the properties from unknown file path. Properties unknownPathProperties = PropertyUtility.getProperties("unknown.properties"); System.out.println("Unknown Path Properties: " + unknownPathProperties + "\n"); } }Output:
File </test.properties> found in the classpath. Class Path Properties: {tp2=value2, tp1=value1} File <test2.properties> found in the FileSystem. Relative Path Properties: {t2p1=value1, t2p2=value2} File <C:\Users\RangaReddy\Ranga_Utilities\test2.properties> found in the FileSystem. Absolute Path Properties: {t2p1=value1, t2p2=value2} File </test.properties> found in the classpath. File <test2.properties> found in the FileSystem. Multiple file Properties: {t2p1=value1, tp2=value2, tp1=value1, t2p2=value2} Exception in thread "main" java.io.FileNotFoundException: File <unknown.properties> is not found. at com.ranga.utility.PropertyUtility.getProperties(PropertyUtility.java:38) at com.ranga.utility.PropertyUtilityTest.main(PropertyUtilityTest.java:38)Happy Learning ...!