Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Find TimeStamp in a string  RSS feed

 
Jivan Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've a text file in which each entry contains a timestamp in the line. I wants to get how old the entry is. for this I need to get the written timestamp from the line. I want to make a generalized function to get the timestamp from the line that is in the format "MM/dd/yyyy hh:mm:ss a". Please anybody help me how I can get the date from the string.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.text.SimpleDateFormat
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are trying to find where the date is located in the string, perhaps regex could help.
 
Jivan Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Norm Radder:
If you are trying to find where the date is located in the string, perhaps regex could help.


Can you help me to find the exact regular expression that can get the string representing the timestamp in the specified line.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just learning regex myself
Given that the format of the time stamp is: MM/dd/yyyy hh:mm:ss a

Here's a first pass at a regex

[ August 05, 2008: Message edited by: Norm Radder ]
 
Jivan Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Norm Radder:
I'm just learning regex myself
Given that the format of the time stamp is: MM/dd/yyyy hh:mm:ss a

Here's a first pass at a regex


[ August 05, 2008: Message edited by: Norm Radder ]


It seems that the regular expression "[0-1]\\d/[0-3]\\d/[1-2]\\d{3}[ ][0-1]\\d[:][0-5]\\d[:][0-5]\\d[ ](A|P)" is working for my problem but it requires month less than 10 to be proceeded with '0' always. can we modify the expression to make it optional for day month not necessarily proceeded with '0' for the specific case (when day date and month value is 1-9).
 
Jivan Goyal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solution to the problem is : "[0-1]{0,1}\\d/[0-3]{0,1}\\d/([1-2]\\d{1}){0,1}\\d{2}[ ][0-1]\\d[:][0-5]\\d[:][0-5]\\d[ ](AM|PM)"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!