Header Ads Widget

Responsive Advertisement

Find and Mask Different Date Formats in a String Using Java

 

To find different date formats in a string and replace them with a mask character (e.g., #), you can use regular expressions (regex) in Java. The idea is to identify various date formats using regex patterns, then replace them with a masked version. Java's Pattern and Matcher classes help in finding and replacing these patterns.


package com.kartik.date.format;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

public class RegexMatches {

 

    private static final String[] formats = {

        "((1[0-2]|0?[1-9]):[0-5][0-9](:[0-5][0-9])?(\\.[0-9]{1,3})?(\\s)?(?i)(AM|PM))", // 12-hour format

        "(([0-1][0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?(\\.[0-9]{1,3})?)", // 24-hour format

        "(\\b(0?[1-9]|[12][0-9]|3[01])[/-](0?[1-9]|1[012])[/-](19|20)?\\d{2}\\b)", // Date in DD/MM/YYYY or DD/MM/YY

        "(\\b(0?[1-9]|1[012])[/-](0?[1-9]|[12][0-9]|3[01])[/-](19|20)?\\d{2}\\b)"  // Date in MM/DD/YYYY or MM/DD/YY

    };

 

    public static void main(String[] args) {

        String line = "This is Kartik Born was 25/02/1986 placed at Saharda for QT3000! OK? and died on 03/24/2015 Mandal Exact time 24-02-1986 10:10:30.0 AM card number xxxx, $ 00,00, on 05/10 or 11/10/17 02:24:00 PM . If you don't recognize call 40032 2412. 11/11 22:24:00";

 

        for (String format : formats) {

            Pattern pattern = Pattern.compile(format);

            Matcher matcher = pattern.matcher(line);

 

            // Replace all matched patterns with an empty string

            line = matcher.replaceAll("");

        }

 

        System.out.println(line);

    }

}

 

This RegexMatches class is designed to remove specific date and time patterns from a given string using regular expressions. The code scans the string, finds matches according to a set of predefined patterns, and replaces those matches with an empty string. Let's break down and analyze the code:

Key Components

  1. Regex Patterns: The formats array contains a variety of regular expressions (regex) designed to match different date and time formats. These include common patterns like:
    • Time with AM/PM: e.g., 10:10:30 AM
    • 24-hour time: e.g., 14:30
    • Date in different formats: e.g., 25/02/1986, 03/24/2015, 24-02-1986, etc.
  2. Pattern Matching and Replacement: The code iterates through each pattern, applies it to the input string using Matcher, and replaces any found matches with an empty string.

Code Analysis:

  1. Regex Patterns: Simplified and clarified the regex patterns, reducing complexity and improving readability.
  2. Efficiency: Instead of creating and managing an array for matches, the code now uses matcher.replaceAll("") to replace all occurrences in one step, which is more efficient.
  3. Code Clarity: Removed unnecessary variables and loops, making the code easier to understand and maintain.

 

Another one approach:

package com.kartik.date.format;

 

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

/**

 *

 * @author kmandal

 *

 */

public class RegexMatches {

 

private static final String[] formats = {

                  "((1[0-2]|[0-2][1-9]):[0-5][0-9](:[0-5][0-9])?(.[0-9]?[0-9]?[0-9])?(\\s)?(?i)(AM|PM|am|pm))",

                  "(([0-1][1-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?(.[0-9]?[0-9]?[0-9])?)",

                  "(?i)(on|On|ON|Value Date:|Effective Date:|Effective On:|date --|date:|--)?(\\s)?(([(0-3)|x|X])([(0-3)|x|X]))([/-])((0?[1-9])|(1[0-2]))([/-])(19|20)?([x|X|\\d][x|X|\\d])?",

                  "(?i)(on|On|ON|Value Date:|Effective Date:|Effective On:|date --|date:|--)?(\\s)?(0?[x|X|(1-9)]|[12][0-9]|3[01])([/-])(0?([1-9]|1[012]))([/-])((19|20)[x|X|\\d][x|X|\\d])",

                  "(?i)(on|On|ON|Value Date:|Effective Date:|Effective On:|date --|date:|--)?(\\s)?(0?[x|X|(1-9)]|1[012])([/-])(0?([1-9]|[12][0-9]|3[01]))([/-])((19|20)[x|X|\\d][x|X|\\d])",

                  "(?i)(on|On|ON|Value Date:|Effective Date:|Effective On:|date --|date:|--)?(\\s)?(0?[x|X|(1-9)]|[12][0-9]|3[01])([/-])((0?[1-9])|(1[0-2]))([/-])([x|X|\\d][x|X|\\d])?",

                  "(?i)(on|On|ON|Value Date:|Effective Date:|Effective On:|date --|date:|--)?(\\s)?(0?[x|X|(1-9)]|[12][0-9]|3[01])([/-])([0-9][0-9])(([/-])([x|X|\\d][x|X|\\d]))?",

                 

                  };

 

 public static void main(String args[]) {

  // String to be scanned to find the pattern.

  String line = "This is Kartik Born was 25/02/1986 placed at Saharda for QT3000! OK? and died on 03/24/2015 Mandal Exact time 24-02-1986 10:10:30.0 AM card number xxxx, $ 00,00, on 05/10 or 11/10/17 02:24:00 PM . If you don't recognize call 40032 2412. 11/11 22:24:00";

 

  // Create a Pattern object

  for (int i = 0; i < formats.length; i++) {

   int k = 0;

   String[] convertArray = new String[100];

   String replaceDate;

   Pattern pattern = Pattern.compile(formats[i]);

   // Now create matcher object.

   Matcher match = pattern.matcher(line);

   while (match.find()) {

    convertArray[k] = match.group();

    k++;

   }

 

   for (int j = 0; j < k; j++) {

    replaceDate = "";

    line = line.replaceAll(convertArray[j], replaceDate);

   }

  }

  System.out.println(line);

 

 }

}

 

 

Both the class give the same out put

Output:

This is Kartik Born was ++++ placed at Saharda for QT3000! OK? and died ++++ ++++ Mandal Exact time ++++ ++++ card number xxxx, $ 00,00, ++++ ++++ or ++++ ++++ . If you d++++'t recognize call 40032 2412. ++++ ++++

 

 

 

date masking
date masking 

 

Post a Comment

0 Comments