Convert 15 digit number string to java8 timestamp - datetime

I am struggling to convert a big 15 digit number string to a date format.
Sample code :
String dateInString = "201410051252323";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssZ");
LocalDateTime dateTime = LocalDateTime.parse(dateInString, formatter);
I am getting following exception
java.time.format.DateTimeParseException: Text '2014100591523' could not be parsed at index 0
Can anyone please suggest the best way to do this in Java 8

You have the right the idea, but there are two issues with your code--one syntactic, and the other logistic:
Your pattern expects the string to be formatted with hyphens and colons and spaces. For example, "yyyy-MM-dd" will match "2014-10-05", but not "20141005". To match the latter, just drop the hyphens and use "yyyyMMdd".
You use a 'Z' in your pattern for the final digit, indicating that you expect a time zone offset in your string. But the 'Z' pattern matches offsets like "+0000" and "-8000", not a single digit. Also, to represent datetimes with a time zone, you should use the ZonedDateTime or OffsetDateTime class instead of LocalDateTime
I'm not sure which time zone the '3' in your example string is supposed to represent, if it is indeed supposed to represent a zone offset at all. If you do not have control over the formatting of your data set, you'll have to peel off the final digit and handle it separately.
String dateInString = "201410051252323";
String dateTimeString = dateInString.substring(0, 14); // "20141005125232"
String zoneDigitString = dateInString.substring(14); // "3"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); // no "Z"
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
/* Manually convert zoneDigitString to a ZoneId here */
ZoneId zone = ...;
ZonedDateTime zonedDateTime = dateTime.atZone(zone);
You'll have to handle the "..." part on your own, depending on what "3" represents.

Related

Hive: Convert string datetime with missing seconds in "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

I'm using the following code to convert a string datetime variable to datetime, but the converted string is missing SSS part.
Code used:
cast(FROM_UNIXTIME(UNIX_TIMESTAMP(oldtime, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss.SSS") as timestamp) as newtime
The outcome:
2019-03-08T18:28:36.901Z is converted to 08MAR2019:18:28:36.000000
Some other oldtimes in string:
2020-03-09T16:05:06:827Z
2020-03-09T16:03:19:354Z
2020-03-11T16:03:57:280Z
2020-03-10T16:02:57:642Z
2020-03-10T16:04:07:455Z
2020-03-10T16:04:09:737Z
2020-03-10T16:03:57:280Z
2020-03-10T16:02:46:816Z
The SSS part '901' is missing in the converted time. Would like help on keeping the SSS part since I need to sort the records by their exact time.
Thank you!
from_unixtime is always until minutes(yyyy-MM-dd HH:mm:ss) to get millisecs we need to do some workarounds.
we will extract the millisecs from the old_time using regexp_extract then concat that to from_unixtime result and finally cast to timestamp.
Example:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\.(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41.335Z")old_time)e
+------------------------+-----------------------+
|old_time |newtime |
+------------------------+-----------------------+
|2020-03-11T21:14:41.335Z|2020-03-11 21:14:41.335|
+------------------------+-----------------------+
UPDATE:
Your sample data have : before milliseconds, Try with below query:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\:(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41:335Z")old_time)e
Simply replace 'T' with space ' ' remove 'Z' and replace last ':' with dot, like this :
select regexp_replace('2020-03-09T16:05:06:827Z','(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3');
Result:
2020-03-09 16:05:06.827
Read also this answer if you need to convert to different format, preserving milliseconds: https://stackoverflow.com/a/59645846/2700344

U-SQL Text Extractor Integer

I need to extract data from a file, see code below.
#rows =
EXTRACT booking_date string,
route string,
channel string,
pos string,
venta string,
flight_date string,
ancillary string,
paxs int?
FROM "/ventas/ventas1.csv"
USING Extractors.Text(delimiter:';', silent:true);
#output =
SELECT booking_date,
channel,
Convert.ToDouble(venta.Replace(",", ".")) AS venta,
paxs
FROM #rows;
My problem is that the numbers are in Spanish format, meaning "100,234" instead of "100.234".
Does anyone know how to change the format in Extractors.Text, or how transform strings in integers in U-SQL?
Import the column as a string and replace the full stops, eg something like this:
#input =
EXTRACT venta string,
paxs int
FROM #inputFile
USING Extractors.Text(delimiter : ';');
#output =
SELECT Convert.ToInt32(venta.Replace(".", "")) AS venta
FROM #input;

How to convert string array by getting numeric value from comma separated string variable?

For example I have a string variable like this:
string data = "12,false,13,false,14,false,15,false";
I'd like to convert it to a string array by filtering numeric value only
string[] datas=["12","13","14","15"]
You can do it using string.Split(), char.IsDigit and linq:
string[] datas = data.Split(',').Where(s => s.All(char.IsDigit)).ToArray();
How it works:
splits the string by ,
filters (Where) the resulting string by checking that
every character in a string is a digit (All, IsDigit)
converts the resulting enumeration into an array (ToArray())

Convert fulltime to minutes C#

I need convert GMT time 14:21:34 to minute (14*60+21=861 minutes)
I know there is a inbuilt function which convert min to HH:MM
Use TimeSpan.FromMinutes:
var result = TimeSpan.FromMinutes(1815);
Is there an easy way to do this that I am missing?
Two options to get to a TimeSpan:
1) Parse it as a DateTime (specifying a format), then use DateTime.TimeOfDay to get a TimeSpan
var dt = DateTime.ParseExact(
"14:21:34",
#"HH\:mm\:ss",
CultureInfo.InvariantCulture);
var ts = dt.TimeOfDay;
I like this option as it sounds like the value you've got is meant to be a time of day.
2) Parse it as a TimeSpan:
var ts = TimeSpan.ParseExact(
"14:21:34",
#"hh\:mm\:ss",
CultureInfo.InvariantCulture);
Once you have a TimeSpan, use TotalMinutes (which returns a double) to get the number of minutes. For example:
Console.WriteLine((int) ts.TotalMinutes); // 861

Substraction of integer values in string variables in asp.net

I am developing a website in asp.net and have this problem:
string price = row["Price"].ToString();
string discount = row["Discount"].ToString();
This code is for taking data from a DataTable and it is working correctly. Both are floating point values(12,50.75...). In my program I want to subtract "discount" from "price" and assign the result to a new string. Suppose string price contains 50 and string discount contains 23.5, then I want 26.5 in the new string. How do I accomplish this?
You can you the below code:
string strPrice = row["Price"].ToString();
string strDiscount =row["Discount"].ToString();
string strFinal = (Convert.ToDouble(strPrice) - Convert.ToDouble(strDiscount)).ToString();
string newPrice = (double.Parse(price)-double.Parse(discount)).ToString();
string strDiff = (Convert.ToDouble(row["Price"].ToString()) - Convert.ToDouble(row["Discount"].ToString())).ToString();
you can convert double string variable to double using Convert.ToDouble or using double.Parse() function.
for difference between these two check
double price ,discount,amount;
string result = null;
price = double.Parse(row["Price"].ToString());
discount = double.Parserow["Discount"].ToString());
amount = price - discount;
string result = Convert.ToString(amount);
yes you can do like this way
double amount=(convert.toDouble(row["price"].tostring()))-(convert.toDouble(row["Discount"].tostring()));
string Totalamount=amount.tostring();
try this..
double amount=(convert.todouble(row["price"].tostring()))-(convert.todouble(row["Discount"].tostring()));
You need double.Parse to make them number and perform arithmetic operations on them.
double price = double.Parse(row["Price"].ToString());
double discount = double.Parse(row["Discount"].ToString());
double amount = price - discount;

Resources