2

Assume that the sentence in english:

String s = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";

And the sentence in chinese:

String s = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";

I have try

  String[] words = s.split("\\s+");
  for (int i = 0; i < words.length; i++) {
  System.out.println(words[i]);
  }

And I get for eng sentence

13:10:47,829 INFO  [STDOUT] Your
13:10:47,829 INFO  [STDOUT] Last
13:10:47,829 INFO  [STDOUT] Login
13:10:47,829 INFO  [STDOUT] was
13:10:47,829 INFO  [STDOUT] 2013/10/04
13:10:47,829 INFO  [STDOUT] 13:06:45
13:10:47,829 INFO  [STDOUT] (
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Days,
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Hours,
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Minutes
13:10:47,829 INFO  [STDOUT] )

And for the chinese sentence:

13:11:49,712 INFO  [STDOUT] 您上次登录是
13:11:49,712 INFO  [STDOUT] 2013/10/04
13:11:49,712 INFO  [STDOUT] 13:07:15(
13:11:49,712 INFO  [STDOUT] 0
13:11:49,712 INFO  [STDOUT] 天,
13:11:49,712 INFO  [STDOUT] 0
13:11:49,712 INFO  [STDOUT] 小时
13:11:49,712 INFO  [STDOUT] 4
13:11:49,712 INFO  [STDOUT] 分钟
13:11:49,712 INFO  [STDOUT] )

In the result I can easily get the date, time and integer value from the eng sentences. But when the sentence change to chinese it cannot get the value using same way. Because the number of array after the sentence split was different. Is there any way I can get date, time and integer value from a sentence even in different language and the number of array return after sentence split was different.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
cchua
  • 213
  • 2
  • 6
  • 15
  • for the date and time, you can use regex pattern dddd/dd/dd & dd:dd:dd – Pranalee Oct 04 '13 at 05:22
  • Do you mind show me some example caode for me – cchua Oct 04 '13 at 05:24
  • Your code works for me and gives me the following output(each word on different line): 您上次登录是 2013/10/04 13:06:45 ( 0 天, 0 小时 0 分钟 ) – Juned Ahsan Oct 04 '13 at 05:25
  • @JunedAhsan - You unknowingly or knowingly added a space between `45` and `(`. Thus, it worked for you. Not sure if OP has the same thoughts about adding an additional space there. That's the thing causing the problem. – Rahul Oct 04 '13 at 05:28
  • Yes the code above is working. My problem is how can i get the date, time and integer value from the string even the split sentence return different number of array. And I have added a space between 45 and ( for testing to. – cchua Oct 04 '13 at 05:29
  • @cchua - Have a look at my answer and see if it helps you. – Rahul Oct 04 '13 at 05:46
  • @cchua have posted solution – Pranalee Oct 04 '13 at 06:44

4 Answers4

4
 public static void main(String[] args) {
    String english = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
    String chinese = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";

    String datePattern = "\\d\\d\\d\\d/\\d\\d/\\d\\d"; 
    String timePattern = "\\d\\d:\\d\\d:\\d\\d";

    System.out.println(getMatch(english, datePattern));
    System.out.println(getMatch(english, timePattern));
    System.out.println(getMatch(english, "\\d Days"));
    System.out.println(getMatch(english, "\\d Hours"));
    System.out.println(getMatch(english, "\\d Minutes"));
    System.out.println();
    System.out.println(getMatch(chinese, datePattern));
    System.out.println(getMatch(chinese, timePattern));
    System.out.println(getMatch(chinese, "\\d 天"));
    System.out.println(getMatch(chinese, "\\d 小时"));
    System.out.println(getMatch(chinese, "\\d 分钟"));
}

private static String getMatch(String input, String regex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    if (matcher.find()) {
        return matcher.group();
    } else {
        return "";
    }
}
Pranalee
  • 3,389
  • 3
  • 22
  • 36
  • This method is work...but it will have problem if the string is change into other language. – cchua Oct 04 '13 at 07:01
  • "Things can be made simple but not simpler" – Pranalee Oct 04 '13 at 07:08
  • ya i know too. R.J posted the method is almost complete but still thinking a way to get the integer value for days, hours and minutes – cchua Oct 04 '13 at 07:11
  • what i mean is as 'days', 'hour' this text will vary in each language. So you can't have common code to parse it. You will have to write pattern for each language for matching. – Pranalee Oct 04 '13 at 07:16
1

java.time

Even though the two solutions I am going to provide you with will work with the legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat), keep in mind that this API is outdated and error-prone and therefore, It is recommended to stop using it completely and switch to java.time, the modern date-time API*. Both of the solutions given below use java.time API.

Solution using RegEx and java.time API:

You can use the regex, \d{1,4}\/\d{1,2}\/\d{1,2} \d{1,2}:\d{1,2}:\d{1,2} to retrieve the date-time string from the text. I recommend you explore the links to understand the RegEx and Java RegEx API. You can understand this specific RegEx with help of the following points:

  • \d{1,4}: 1 to 4 digits
  • \/: The character literal, /

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String english = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
        String chinese = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";

        // Assuming the date-time string is in the format, yyyy/MM/dd HH:mm:ss
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/M/d H:m:s");

        // Processing english
        LocalDateTime dt = LocalDateTime.parse(getDateTime(english), dtf);
        System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n", dt.getYear(),
                dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond());

        // Processing chinese
        dt = LocalDateTime.parse(getDateTime(chinese), dtf);
        System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n", dt.getYear(),
                dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond());
    }

    static String getDateTime(String s) {
        Matcher matcher = Pattern.compile("\\d{1,4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}").matcher(s);
        String strDateTime = "";
        if (matcher.find()) {
            strDateTime = matcher.group();
        }
        return strDateTime;
    }
}

Output:

Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45
Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45

ONLINE DEMO

Solution, purely using java.time API:

import java.text.ParsePosition;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Optional;

public class Main {
    public static void main(String[] args) {
        String english = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
        String chinese = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";

        // Processing english
        Optional<LocalDateTime> date = getDateTime(english);
        date.ifPresent(dt -> System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n",
                dt.getYear(), dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond()));

        // Processing chinese
        date = getDateTime(chinese);
        date.ifPresent(dt -> System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n",
                dt.getYear(), dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond()));
    }

    static Optional<LocalDateTime> getDateTime(String s) {
        // Assuming the date-time string is in the format, yyyy/MM/dd HH:mm:ss
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/M/d H:m:s");
        
        Optional<LocalDateTime> result = Optional.empty();
        for (int i = 0; i < s.length(); i++) {
            try {
                result = Optional.ofNullable(LocalDateTime.from(dtf.parse(s, new ParsePosition(i))));
                break;
            } catch (DateTimeParseException | IndexOutOfBoundsException e) {
            }
        }
        return result;
    }
}

Output:

Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45
Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45

ONLINE DEMO

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Here something you can do to find out the Date and the Time. You can follow on the same lines to get the Integer values too(though I still don't know which Integer values you're talking about).

String s = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
String[] words = s.split("\\s+");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String datePlusTime = null;
for (int i = 0; i < words.length; i++) {
    try {
        sdf.parse(words[i]);
        // It comes here, that means its the date we want
        datePlusTime = words[i] +" "+ words[i + 1]; // This has date plus the time
        // I concatenated them just for an example. You can do whatever you want with them.
        break;
    } catch (ParseException pe) {
        // Eat the exception
        // I must say this is not a good practice though
    }
}

System.out.println(datePlusTime); // Do whatever you want with it.

P.S:- This is assuming that there is a space between 45 and the (. If there is not, then you need to remove the last character of the words[i+1] string accordingly.

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • the integer value mean the 0 days, 0 hours, 0 minutes. the 0 is the integer i wan to get too. – cchua Oct 04 '13 at 06:52
0

If your string is static then you can try like below.

 String s = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";
 System.out.println( s.substring(6, 17)  + "  -- " +   s.substring(18,26) + "-- " +s.substring(28, 29) + "-- " +s.substring(33, 34) + "-- " +s.substring(38, 39));

May be this is not a good one.

Dhinakar
  • 4,061
  • 6
  • 36
  • 68