Unable to convert date with UTC using Joda Time API [closed] - java-8

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I'm trying to convert this date into local date
Input: "2021-04-20T15:00:00+02:00";
Expected output: "2021-04-20T13:00:00Z";
Actual output : "2021-04-20T15:00:00
Can you please let me know which library to use?
Code:
String date = "2021-04-20T15:00:00+02:00";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
LocalDateTime dt = LocalDateTime.parse(date, formatter);
System.out.println(dt.toString());

You can use java-8 OffsetDateTime to parse the input string (since it is in ISO-8601) and then use toInstant
String timeStamp = "2021-04-20T15:00:00+02:00";
OffsetDateTime.parse(timeStamp).toInstant() //2021-04-20T13:00:00Z

tl;dr
You used the wrong type, LocalDateTime. Use OffsetDateTime and Instant.
OffsetDateTime
.parse( "2021-04-20T15:00:00+02:00" )
.toInstant()
.toString()
Details
The Joda-Time library is now In maintenance mode after having been succeeded years ago by the java.time classes defined in JSR 310 and built into Java 8 and later.
Your input has an indicator of an offset-from-UTC of +02:00, two hours ahead of UTC. So parse as a java.time.OffsetDateTime object.
OffsetDateTime odt = OffsetDateTime.parse( "2021-04-20T15:00:00+02:00" ) ;
Adjust to UTC by merely extracting an Instant.
Instant instant = odt.toInstant() ;
Generate your desired output.
String output = instant.toString() ;

Related

Laravel How to filter inputs before going to the DataBase? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Laravel How to filter inputs before going to the DataBase
what shoud i do to filter my inputs before saving to the database? i want it to be uniformed in like this format "Name" a uper case followed by lowercase.
in some cases like when the user register a full caps name i want it to be re format as the example "Name"
there's an especific PHP function to do what you need: ucfirst(). This function turns to Uppercase the first char from a string.
For example:
//If $request->name is 'JoHn' or whatever
$filtered_name = ucfirst($request->name);
//Returns 'John'
If is a case with two names, you can use the ucwords() PHP function (Turn first letter uppercase of every word from a string)
//If $request->name is 'OlivER JAMES'
$filtered_name = ucwords($request->name);
//Returns 'Oliver James'
Hope this help you.

Is there a human language that has months ordered alphabetically? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
So I like datestrings in ISO format ie 2021-12-25 - it means you can just apply a simple sort and dates are in the right order.
But sometimes it is nice to see months as English rather trying to remember if October is 8th or ninth or tenth month. (maybe its just me).
So 2021-Dec-25 is nice to read but I lose that sort quality - this date would now appear before Thanksgiving on 2021-Nov-25.
So I was wondering if there was a human language that can do both - is there a language where January comes before February in that language's alphabet. For example French fails here (Janvier > Fevrier).
Well, let's query the cultures: for each culture we get months' names and check if they are sorted (we compare original names with the sorted names).
C# code:
var result = CultureInfo
.GetCultures(CultureTypes.AllCultures)
.Where(ci => ci.DateTimeFormat
.MonthNames
.Where(month => !string.IsNullOrEmpty(month))
.Where(month => !Regex.IsMatch(month, "^M[0-9]+$"))
.Any())
.Where(ci => ci.DateTimeFormat
.MonthNames
.Where(month => !string.IsNullOrEmpty(month))
.SequenceEqual(ci
.DateTimeFormat
.MonthNames
.Where(month => !string.IsNullOrEmpty(month))
.OrderBy(month => month)))
.OrderBy(ci => ci.Name)
.Select(ci => $"{ci.Name} ({ci.EnglishName})");
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
Empty
So, there are no such (lucky?) cultures on the Earth (at least known to .Net 5)
You can try AbbreviatedMonthNames, MonthGenitiveNames, but alas: the output will be empty as well.

Laravel Carbon Date add 7 minutes [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
My request returns a date string
"07/13/2020 8:00 AM - 07/17/2020 5:00 PM"
Then I explode it
$datetimes = explode(' - ', $request->datetimes);
Then I get when I DD
"07/13/2020 8:00 AM"
When I use carbon to format the string
\Carbon\Carbon::parse($datetimes[0])->format('d M, H:mA')
It returns
"13 Jul, 08:07AM"
Why is this happening?
Because it's converting the date from the american format MM/DD/YYYY instead of the common DD/MM/YYYY
That why when handeling date formats, it's safer to use the mysql date format YYYY/MM/DD
it's how strtotime in php works, if the day is a valid month, it will use the MM/DD/YYYY format.
one easy trick is to replace the / with - to force it.
\Carbon\Carbon::parse(str_replace('/', '-', $datetimes[0]))
I was using "m" for month instead of "i" for minutes...
Can you please give this a try
Route::get('convert',function(){
$myString = "07/13/2020 8:00 AM - 07/17/2020 5:00 PM";
$dateTimes = explode(' - ', $myString);
$paseDateTime = \Carbon\Carbon::createFromFormat('m/d/Y g:iA',$dateTimes[0])->addMinutes(7)->format('m/d/Y g:iA');
return $paseDateTime;
});
Output
07/13/2020 8:07AM

In FreeMarker adding one hour to current timestamp [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
In FreeMarker, I am trying to add one hour to a timestamp which is in the format "2016-08-11T20:06+06:00".
I have tried and searched on Google, but didn't find way to add hours to the current timestamp. The new timestamp should be current system timestamp plus one hour.
I am getting the following error.
For "." left-hand operand: Expected a hash, but this has evaluated to a date_or_time_or_datetime (wrapper: f.t.SimpleDate):
==> current [in nameless template at line 2, column 19]
FTL stack trace ("~" means nesting-related):
- Failed at: #assign newDate = current.add(10, 1) [in nameless template at line 2, column 1]
<#assign current= .now> <#assign newDate= current.add(10,1) > ${newDate}
I have tried this but getting above error. Please help me out.

Ruby snippet for reading lines block [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am new to ruby and seeking help.
We got the csv files having the column with data shown below. Using the csv parser and fetch the column data for each in a variable.
And we want to change column data from csv file to line shown below and write to a file:
FROM
---
- - status
- New
- Delivered
TO
status from New to Delivered
Thanks #igian for help
---
- - status
- New
- Delivered
- - Milestone
- Sprint1
- Sprint
I was struggling for this. I tried to use y.second but it fails, please correct what I am doing wrong.
Looks like a YAML file, see https://ruby-doc.org/stdlib-2.6.2/libdoc/yaml/rdoc/YAML.html
Load the file then handle the array:
require 'yaml'
y = YAML.load_file( 'the_file.yaml' )
y #=> [["status", "New", "Delivered"]]
words = y.first
p "#{words[0]} from #{words[1]} to #{words[2]}"
#=> "status from New to Delivered"

Resources