Converting UTC Date to GMT in Groovy / Java - groovy

I am working with SoupUI a i need to adjust a Date/time (UTC) that i get back in a response to a GMT Date/time. The date that i get back in the respone looks as followes:
2012-11-09T00:00:00+01:00
I would like to convert this to
2012-11-08T23:00:00Z
Unfortunatly i lack Java skils and therefore also Groovy skils to be able to do this on my own. i did a lot o searches on date convertions but until now i was still unable to find what i was looking for. i will keep searching. if i do manage to get the solution then i will post it here.

Assuming there isn't a colon in the timezone portion, I believe this should work:
// Your input String (with no colons in the timezone portion)
String original = '2012-11-09T00:00:00+0100'
// The format to read this input String
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ" )
// The format we want to output
def outFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'" )
// Set the timezone for the output
outFormat.timeZone = java.util.TimeZone.getTimeZone( 'GMT' )
// Then parse the original String, and format the resultant
// Date back into a new String
String result = outFormat.format( inFormat.parse( original ) )
// Check it's what we wanted
assert result == '2012-11-08T23:00:00Z'
If there is a colon in the TimeZone, you'll need Java 7 for this task (or maybe a date handling framework like JodaTime), and you can change the first two lines to:
// Your input String
String original = '2012-11-09T00:00:00+01:00'
// The format to read this input String (using the X
// placeholder for ISO time difference)
def inFormat = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssX" )

Related

Reformatting date values when using them as URL parameters in a PowerQuery API request

I have two dates in my Excel table with the following format: "dd-mm-yyyy". These dates need to be sent as URL query parameters to an API endpoint for getting some data using PowerQuery. However, the API endpoint does not accept dates in that format. Therefore, I need to convert them to the format "mm-dd-yyyy" instead for it to work.
For getting the values from my table, I use the following code:
let GetNamedRange=(NamedRange) =>
let
name = Excel.CurrentWorkbook(){[Name=NamedRange]}[Content],
value = name{0}[Column1]
in
value
in
GetNamedRange
This function, called "GetValue", is then called when inserting URL query parameters in my GET request:
Csv.Document(Web.Contents("my.api/leave/leavecsv", [Query = [periodStart = GetValue("periodStart"), periodEnd = GetValue("periodEnd"), department = GetValue("department")]]),[Delimiter=";", Columns=14, Encoding=1252, QuoteStyle=QuoteStyle.None])
Currently the cells for my dates are in Text format. I tried using Date.FromText(...) to format the dates, but I get an error saying the datetime format is invalid.
https://learn.microsoft.com/en-us/powerquery-m/date-fromtext
How can I propertly format my date values before inserting them as URL query parameters using PowerQuery?
Ensure your dates are real dates and set to type date. then you can use the Date.ToText function:
let
theDate = #date(2022,12,7),
output = Date.ToText(theDate,"MM-dd-yyyy")
in
output
If, for some reason, you must maintain your dates as text strings (I'd like to know why, if that's the case), you can convert them first to a "real" date, and then create the string:
let
theDate = "13-12-2022",
output = Date.ToText(Date.FromText(theDate, "en-150"),"MM-dd-yyyy")
in
output
Make sure you pass in a culture and format. i.e.
Date.FromText([Column1], [Format="dd-MM-yyyy", Culture="en-UK"])

How to convert date to the specified string format in nodejs?

I'm reading a file's modification date with fs.statSync() and I'd like to compare it with a String that comes from my database:
This is my file's last modification date:
Fri Mar 24 2017 13:22:01 GMT+0100 (Central Europe Standard Time)
And I'd like to compare with this string:
2016-07-18 12:28:12
How can I do this with Node.JS? Can I somehow create a new date from the String?
You can use Moment parse both of those formats and compare the results.
var date1 = moment(string1, format1, true);
var date2 = moment(string2, format2, true);
if (date1.diff(date2) === 0) {
// equal
} else {
// not equal
}
See this answer for parsing dates in different formats:
Check if a string is a date value
Make sure that you parse it with explicit format and you don't let it guess the format, or otherwise you will never be sure if it guessed correctly.

Removing time format in a date

I am passing a date as a parameter where daydate is the parameter
string daydate
The content of daydate is 3/3/2017 12:00:00 AM
I am doing this to remove the time and still the time does not go away
string strCleanDate = String.Format("{0:M/d/yyyy}", daydate);
How can I remove the time?
That is because the formatter is ignored as the instance you are passing to string.Format is a string so the placeholder is filled with the value you passed in and the format is ignored completely. If you want to apply DateTime formatting you need to pass in an instance of a DateTime type.
Your first fix should be to change your parameter to be of type DateTime and not string. Otherwise a caller could pass "HI THERE" and your method/application would break, or worse push that value to a store and now your store is polluted with invalid value(s).
public void SomeMethod(DateTime daydate)
{
var dateOnly = dayDate.Date;
}
You should pass dayDate as a DateTime value instead of as a string. Then the format will work correctly.

Grails - converting a String to Date and then back to String

I've got this string 21-04-2016 for instance, that I'm trying to convert to a date. After that's done I need to flip it around, remove all the dashes and then convert it back to a string so that it looks like 20160421. I have found some code that I'll post below but I want to know if there is a simple way of doing this, maybe without having to convert the string to a date? Anyhow, here's my (broken) code:
String from = region.startDate //I get this string from a controller
Date fromDate = Date.parse('yyyy-MM-dd', from) //.parse is depricated apparently
println(from)
println(fromDate)
Here's what I get back (from the println):
Wed Jul 08 00:04:00 SAST 16
11-04-2016
No need to convert from String to Date in that case (you could use a SimpleDateFormat (or rather two) to do that, though); just use String and List operations from Java and Groovy:
from.split('-').reverse().join()

How do you parse a date and time string with offset using NodaTime?

I am trying to learn how to use NodaTime within my application, but cannot find very many examples of how to do certain things with this library.
Given:
date/time text of "2012/08/30 17:45:00"
the format string is "yyyy/MM/dd HH:mm:ss"
the date/time offset from UTC is -5
How do I parse this with NodaTime to get an
OffsetDateTime?
Instant?
Using pure NodaTime code, there is not currently a direct parser for an OffsetDateTime. See the documented limitations. However, you can construct one by parsing a LocalDateTime and an Offset separately:
var ldt = LocalDateTimePattern.CreateWithInvariantCulture("yyyy/MM/dd HH:mm:ss")
.Parse("2012/08/30 17:45:00")
.Value;
var o = OffsetPattern.GeneralInvariantPattern
.Parse("-05")
.Value;
var odt = new OffsetDateTime(ldt, o);
There is a similar parser for Instant, but it requires a UTC time - not an offset.
You could also just use the text parsing for DateTimeOffset in the BCL, and then do:
var odt = OffsetDateTime.FromDateTimeOffset(dto);
Either way, once you have an OffsetDateTime, it is convertible to an Instant:
var instant = odt.ToInstant();

Resources