I have a file path name whose prefix always changes as below :
"Unregistered_2018-05-02_14.40.04_+621241411112_34243555523.mp3"
"Martin_2018-04-01_03.10.40_+111_5213441935.mp3"
"Byan_2018-01-04_04.70.01_+62994_2313325553.mp3"
How can I retrieve date (2018-01-04), time (04.70.01) and number phone (+111) with the ever-changing data ?
Whoever you are I am very grateful to finish this
You can use split with _ like this :
String[] texts = new String[] {
"Unregistered_2018-05-02_14.40.04_+621241411112_34243555523.mp3",
"Martin_2018-04-01_03.10.40_+111_5213441935.mp3",
"Byan_2018-01-04_04.70.01_+62994_2313325553.mp3",
};
for (String text : texts) {
String[] split = text.split("_");
String date = split[1];
String time = split[2];
String phone = split[3];
System.out.println("date = " + date + ", time = " + time + ", phone = " + phone);
}
Outputs
date = 2018-05-02, time = 14.40.04, phone = +621241411112
date = 2018-04-01, time = 03.10.40, phone = +111
date = 2018-01-04, time = 04.70.01, phone = +62994
Related
I have a static ArrayList being populated at startup from Firebase to store a variety of values. One of the FB fields I need to store is a Timestamp.
I need to store the Timestamp in my array, but then display it later in a readable Date format.
Initialise the ArrayList:
static ArrayList<String> suspectedValues = new ArrayList();
Get the timestamp from FB:
Timestamp dtg = (Timestamp) document.getData().get("dtg");
Set the timestamp as a String and store in the Array:
String timeStamp = String.valueOf(dtg);
String suspectedData [] = {timeStamp};
suspectedValues.addAll(Arrays.asList(suspectedData));
Later in a different activity onCreate:
int counter = 0;
for(int i = 0; i < suspectedValues.size(); i = i + 4){
String timeStamp = suspectedValues.get(4);
Date dtg = new Date(Long.parseLong(timeStamp));
gMap.addMarker(new MarkerOptions().position(latLng).title(suspected).snippet("id: " + id + " | " + taggedAt + ": " + dtg).icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
if(suspectedValues.size() != 0){
counter = counter + 4;
}
}
I have tried a number of different conversions between Timestamp, Date and String but cant get this to work. When I try to set the Date dtg I get:
java.lang.NumberFormatException: For input string: "56.504800874303676"
at java.lang.Long.parseLong(Long.java:594)
at java.lang.Long.parseLong(Long.java:636)
I have also tried:
Timestamp dtg = (Timestamp) document.getData().get("dtg");
Date date = dtg.toDate();
String timeStamp = date.toString();
String suspectedData [] = {timeStamp};
suspectedValues.addAll(Arrays.asList(suspectedData));
with:
String timeStamp = suspectedValues.get(4);
Date theSameDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(timeStamp);
But it keeps throwing a Parse exception.
Timestamp from FB: Timestamp(seconds=1583706187, nanoseconds=895000000)
Converted to Date: Sun Mar 08 22:23:07 GMT 2020
timestamp String going into Array: Sun Mar 08 22:23:07 GMT 2020
timestamp String coming out of Array: 62.76105524614817
I/MapsActivity: ParseException: java.text.ParseException: Unparseable date: "62.76105524614817"
Ok, I figured a workaround,
I created two separate ArrayLists, one accepts String and the 2nd accepts Date.
static ArrayList<String> suspectedValues = new ArrayList();
static ArrayList<Date> suspectedDateValues = new ArrayList<>();
I put all my Dates in the 2nd ArrayList instead of the 1st.
I also created 2 counters.
int counter = 0;
int counter1 = 0;
I know that I have a date after every 3 values in the first ArrayList. So as I iterate through the 1st list, I call a new method, passing in all the values from the 1st method, which retrieves the date from the 2nd ArrayList and uses the date alongside the passed values to execute my tasks.
for(int i = 0; i < suspectedValues.size(); i = i + 3){
[snip]
getSuspectedDate(lat, lng, gMap, counter1, passedActivity, id, smallMarker);
if(suspectedValues.size() != 0){
counter = counter + 3;
counter1 = counter1 + 1;
}
}
And my final method executed on each iteration of the for loop above:
public void getSuspectedDate(double lat, double lng, GoogleMap gMap, int counter1, Activity passedActivity, String id, Bitmap smallMarker){
String suspected = passedActivity.getResources().getString(R.string.suspected);
String taggedAt = passedActivity.getResources().getString(R.string.taggedDTG);
Date date = suspectedDateValues.get(counter1);
String dtg = String.valueOf(date);
LatLng latLng = new LatLng(lat, lng);
gMap.addMarker(new MarkerOptions().position(latLng).title(suspected).snippet("id: " + id + " | " + taggedAt + ": " + dtg).icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
}
Not the cleanest solution, but it works!
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 trying to retrieve the full file path from the classes that have been edited. I can retrieve the comments, version number, author and date but can't seem to get the full file path that has been edited when more than one class has been included into the project.
I use these to retrieve the Comments and Author:
String comments = pc.getPendingChanges().getComment();
System.out.println("Comments: " + comments);
String author = pc.getPendingChanges().getAllPendingChanges()[0].getPendingSetOwnerDisplay();
System.out.println("Author: " + author);
I use this to retrieve the path:
String fileName = pc.getPendingChanges().getAllPendingChanges()[0].getLocalItem();
System.out.println("FileName: " + fileName);
but I only get this output:
FileName: C:\VS2013\Plugin\PluginTest\HelloWorld.classpath
I need to display the full path with the class name eg
FileName: C:\VS2013\Plugin\PluginTest\HelloWorld.Test.java
Full method displayed below:
#Override
public PolicyFailure[] evaluate(PolicyContext context)
throws PolicyEvaluationCancelledException {
final PendingCheckin pc = getPendingCheckin();
final List<PolicyFailure> failures = new ArrayList<PolicyFailure>();
final WorkItemCheckinInfo[] AssociatedWorkItems = pc.getWorkItems().getCheckedWorkItems();
WorkItem AssociatedCodeReview = null;
String failureReason = "";
for (int i=0; i<AssociatedWorkItems.length; i++) {
AssociatedCodeReview = AssociatedWorkItems[i].getWorkItem();
if (AssociatedCodeReview.getType().getName() == "Code Review") {
break;
}
}
if (AssociatedCodeReview != null) {
if (AssociatedCodeReview.getFields().getField("System.State").getValue()
.toString().equals("Not Approved")) {
failureReason = "Code Review Work Item associated but that is not approved by expert";
failures.add(new PolicyFailure(failureReason, this));
}
} else {
String fileName = pc.getPendingChanges().getAllPendingChanges()[0].getLocalItem();
System.out.println("FileName: " + fileName);
String fileName2 = pc.getPendingChanges().getAllPendingChanges()[0].getServerItem();
System.out.println("FileName2: " + fileName2);
String[] fileName3 = pc.getPendingChanges().getAffectedTeamProjectPaths();
System.out.println("FileName3: " + fileName3[0]);
//System.out.println("Found " + pc.getPendingChanges().getAffectedTeamProjectPaths()[0] + " work items.");
String comments = pc.getPendingChanges().getComment();
System.out.println("Comments: " + comments);
String author = pc.getPendingChanges().getAllPendingChanges()[0].getPendingSetOwnerDisplay();
System.out.println("Author: " + author);
String version = String.valueOf(pc.getPendingChanges().getAllPendingChanges()[0].getVersion());
System.out.println("Version: " + version);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println("Date Time: " + dateFormat.format(date));
}
return failures.toArray(new PolicyFailure[failures.size()]);
}
It actually does work, it was an error with my Eclipse install.
String fileName = pc.getPendingChanges().getAllPendingChanges()[x].getLocalItem();
Edited question
I want to pull the date and time out of some strings. Here's an example. All Event strings start with [0r(1)2[000p[040qe1w3h162[020t*. upon encountering a new one, it should parse the last string set and get some data. an example event is below
[0r(1)2[000p[040qe1w3h162[020t*881*11/11/2010*12:24*
*EVENT STARTED*
[020t 12:24:06 SMARTCARD ENTERED
11\11\10 12:24 10390011
123456789098765432 6598
INVALID TRANSACTION, PLEASE CONTACT
ADMIN FOR ADVICE
-----------------------------------
[020t 12:24:52 FILE STACKED
[020t 12:24:59 FILE PRESENTED 0,5,0,0
[020t 12:25:03 FILE TAKEN
11\11\10 12:25 10390011
123456789098765432 6599
WITHDRAW FILES10.00
[000p[040q(1 *6599*1*E*000050000,M-00,R-10200
-----------------------------------
[020t 12:25:34 SMARTCARD TAKEN
[020t 12:25:38 EVENT ENDED
I want to extract date and time as one variable for every activity. e.g.
Activity= EVENT STARTED
Activity time/date= 11/11/2010 12:24
Activity= SmartCard inserted
Activity time/date= 12:24:06
I tried the following
/*
String sample = "[0r(1)2[000p[040qe1w3h162[020t*882*11/11/2010*12:26*";
String regex = "(?x) ^([0r(1)2[000p[040qe1w3h162[020t*):// ([^/:]+) (?:(\\d+))?";
Matcher m = Pattern.compile(regex).matcher(sample);
if(m.find())
{
String ignore = m.group();
String date = m.group(1);
String time = m.group(2);
System.out.println( date + " " + time);
}
*/
//this section isn't useful in light of the edit to the question
Use String.split(String regex):
String line = "[0r(1)2[000p[040qe1w3h162[020t*882*11/11/2010*12:26*";
String[] parts = line.split("\\*");
String date = parts[2];
String time = parts[3];
System.out.println("date=" + date + ", time=" + time);
Output:
date=11/11/2010, time=12:26
class sql
{
public static void main (String [] args)
{
String dateInCase = "11/11/2010";
String termID;
String line = " 11\11\10 12:24 10390011";
String[] parts = line.split("");
String termId = parts[4]+parts[5]+parts[6]; //to catch terminal ID
String cardInserted = parts[1]+parts[2]+parts[3]+parts[4]+parts[5];
String starter = parts[4]+parts[7]+parts[13]+parts[14]+parts[15];
String tracker = parts[3]+parts[4]+parts[5]+parts[6]+parts[7];
boolean V = (termId.matches("\\s\\d\\d"));
boolean W = (cardInserted.matches("\\s\\s\\s\\s\\s"));//this gets card inserted
boolean X = (starter.matches("\\D\\d\\d\\d\\d"));// a new event set has started
boolean Y = (tracker.matches("\\d\\d\\d\\D\\s")); // this checks for any activity as all activities have numbers in 3,4,5
System.out.println(V);
System.out.println(W);
System.out.println(X);
System.out.println(Y);
if(V == true)
{
parts = line.split("\\ ");
String terminal = parts[2];
System.out.println("terminal " + terminal);
}
if(W == true)//this gets card inserted strings
{
parts =line.split("\\*");
String activity = parts[1];
System.out.print(activity);
}
if(X == true) //action if it sees a new event set
{
parts = line.split("\\*");
String datetime = parts[2]+" "+ parts[3];
System.out.println("time= " + datetime);
dateInCase = parts[2];
}
if(Y == true) //action if it sees a new event
{
parts =line.split("\\ ");
String datetime = dateInCase+ " " + parts[1];
String activity = parts[2]+ " " + parts[3];
System.out.println("time= " + datetime + " activity= " + activity);
}
}
}
I want to extract dates with different formats out of web pages. I am using the Selenium2 Java API to interact with the browser. Also i use jQuery to further interact with the document. So, solutions for both layers are welcome.
Dates can have very different formats in different locales. Also, month names can be written as text or as number. I need to match as much dates as possible, and I am aware of the fact that there are many combinations.
For example if I have a HTML element like this:
<div class="tag_view">
Last update: May,22,2011
View :40
</div>
I want that the relevant part of the date is extracted and recognized:
May,22,2011
This should now be converted to a regular Java Date object.
Update
This should work with the HTML from any web page, the date can be contained in any element in any format. For example here on Stackoverflow the source code looks like this:
<span class="relativetime" title="2011-05-13 14:45:06Z">May 13 at 14:45</span>
I want it to be done the most effective way and i guess this would be a jQuery selector or filter which returns a standardized date representation. But I am open to your suggestions.
Since we can't limit ourselves to any specific element type or children of any element, you're basically talking about searching the whole page's text for dates. The only way to do this with any kind of efficiency is to use regular expressions. Since you're looking for dates in any format, you need a regex for each acceptable format. Once you define what those are, just compile the regexes and run something like:
var datePatterns = new Array();
datePatterns.push(/\d\d\/\d\d\/\d\d\d\d/g);
datePatterns.push(/\d\d\d\d\/\d\d\/\d\d/g);
...
var stringToSearch = $('body').html(); // change this to be more specific if at all possible
var allMatches = new Array();
for (datePatternIndex in datePatterns){
allMatches.push(stringToSearch.match(datePatterns[datePatternIndex]));
}
You can find more date regexes by googling around, or make them yourself, they're pretty easy. One thing to note: You could probably combine some regexes above to create a more efficient program. I'd be very careful with that, it could cause your code to become hard to read very quickly. Doing one regex per date format seems much cleaner.
You could consider using getText to get element text and then split the String, like -
String s = selenium.getText("css=span.relativetime");
String date = s.split("Last update:")[1].split("View :")[0];
I will answer this myself because i came up with a working solution. I appreciate comments though.
/**
* Extract date
*
* #return Date object
* #throws ParseException
*/
public Date extractDate(String text) throws ParseException {
Date date = null;
boolean dateFound = false;
String year = null;
String month = null;
String monthName = null;
String day = null;
String hour = null;
String minute = null;
String second = null;
String ampm = null;
String regexDelimiter = "[-:\\/.,]";
String regexDay = "((?:[0-2]?\\d{1})|(?:[3][01]{1}))";
String regexMonth = "(?:([0]?[1-9]|[1][012])|(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?))";
String regexYear = "((?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3}))";
String regexHourMinuteSecond = "(?:(?:\\s)((?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):([0-5][0-9])(?::([0-5][0-9]))?(?:\\s?(am|AM|pm|PM))?)?";
String regexEndswith = "(?![\\d])";
// DD/MM/YYYY
String regexDateEuropean =
regexDay + regexDelimiter + regexMonth + regexDelimiter + regexYear + regexHourMinuteSecond + regexEndswith;
// MM/DD/YYYY
String regexDateAmerican =
regexMonth + regexDelimiter + regexDay + regexDelimiter + regexYear + regexHourMinuteSecond + regexEndswith;
// YYYY/MM/DD
String regexDateTechnical =
regexYear + regexDelimiter + regexMonth + regexDelimiter + regexDay + regexHourMinuteSecond + regexEndswith;
// see if there are any matches
Matcher m = checkDatePattern(regexDateEuropean, text);
if (m.find()) {
day = m.group(1);
month = m.group(2);
monthName = m.group(3);
year = m.group(4);
hour = m.group(5);
minute = m.group(6);
second = m.group(7);
ampm = m.group(8);
dateFound = true;
}
if(!dateFound) {
m = checkDatePattern(regexDateAmerican, text);
if (m.find()) {
month = m.group(1);
monthName = m.group(2);
day = m.group(3);
year = m.group(4);
hour = m.group(5);
minute = m.group(6);
second = m.group(7);
ampm = m.group(8);
dateFound = true;
}
}
if(!dateFound) {
m = checkDatePattern(regexDateTechnical, text);
if (m.find()) {
year = m.group(1);
month = m.group(2);
monthName = m.group(3);
day = m.group(3);
hour = m.group(5);
minute = m.group(6);
second = m.group(7);
ampm = m.group(8);
dateFound = true;
}
}
// construct date object if date was found
if(dateFound) {
String dateFormatPattern = "";
String dayPattern = "";
String dateString = "";
if(day != null) {
dayPattern = "d" + (day.length() == 2 ? "d" : "");
}
if(day != null && month != null && year != null) {
dateFormatPattern = "yyyy MM " + dayPattern;
dateString = year + " " + month + " " + day;
} else if(monthName != null) {
if(monthName.length() == 3) dateFormatPattern = "yyyy MMM " + dayPattern;
else dateFormatPattern = "yyyy MMMM " + dayPattern;
dateString = year + " " + monthName + " " + day;
}
if(hour != null && minute != null) {
//TODO ampm
dateFormatPattern += " hh:mm";
dateString += " " + hour + ":" + minute;
if(second != null) {
dateFormatPattern += ":ss";
dateString += ":" + second;
}
}
if(!dateFormatPattern.equals("") && !dateString.equals("")) {
//TODO support different locales
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatPattern.trim(), Locale.US);
date = dateFormat.parse(dateString.trim());
}
}
return date;
}
private Matcher checkDatePattern(String regex, String text) {
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
return p.matcher(text);
}