#how to use RFC1123 date in http header?

58 messages · Page 1 of 1 (latest)

oak flare
#

hey guys. can smb explain to me how to use DateTimeFormatter RFC_1123_DATE_TIME? Im making a request to the api and i need to have Date header with its value being in RFC1123 format. can smb help me out? i dont understand what to write here:

HttpHeaders headers = new HttpHeaders();
headers.set();

thanks in advance.

vast roseBOT
#

This post has been reserved for your question.

Hey @oak flare! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

south orbit
#

I mean, it depends on what client API you're using.
In the most general case, there's nothing special, that's like if you wanted to set a numeric header you'd first convert your value to String then set the header to that String. It's the same principle with a date.

weak abyss
#

Use ZonedDateTime to get the current date and time. ZonedDateTime is preferred as it includes the time zone information which is important for the RFC-1123 format.

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.http.HttpHeaders;

public class Example {
    public static void main(String[] args) {
        // Step 1: Get the current date and time
        ZonedDateTime now = ZonedDateTime.now();
        
        // Step 2: Format the date and time using RFC_1123_DATE_TIME
        String formattedDate = now.format(DateTimeFormatter.RFC_1123_DATE_TIME);
        
        // Step 3: Create HttpHeaders and set the Date header
        HttpHeaders headers = new HttpHeaders();
        headers.set("Date", formattedDate);
        
        // Example output
        System.out.println("Formatted Date: " + formattedDate);
        System.out.println("Headers: " + headers);
    }
}
south orbit
oak flare
#

but will it add GMT letters at the end? bc in postman i set my date to Mon, 27 May 2024 12:55:00 GMT, so i need the same for my app

south orbit
#

Well, it depends on whether you'll use spring's internal API for http client.

#

If you want it to use GMT, you should force the ZonedDateTime to be on GMT. Let me check an example

oak flare
#

yep, its Spring functionality

weak abyss
#

and with formatted you will have:

oak flare
#

ah, i need the first version

oak flare
#

so i can ditch it, right?

#

and i can just use this

weak abyss
#

yes

#

But it will show London time zone

#

where GMT starts

oak flare
#

what do u mean?

weak abyss
#

If you are in some another time zone you ill not see added hours

oak flare
#

im gmt +3

#

crap

weak abyss
#

me too

oak flare
#

so whats the solution then?

#

ah wait

#

ok. i see

weak abyss
#

You just need GMT at the end?

#

but with added hours?

#

You can edit the formatted string and just add it manually

oak flare
#

nope. as i understand i dont need added hours, but i need GMT letters appended. for example now its 10:54 on my computer. but if i want to make a request with postman, i need to have this:

weak abyss
#

I edited like this:

oak flare
#

but u have +0300 before GMT letters. i dont need that

weak abyss
#

I see

oak flare
#

i just need to have current GMT time and add GMT letters to the time

#

and thats where im stuck

weak abyss
#

Still you can edit the string yourself and find the +0300 and cut it

#

and create new string

#

simple school level string operations

#

but hours will be added

#

at your time zone

oak flare
#

but wait. i see that this is what i need

#

and i dont need any string operations

weak abyss
#

Yes. But I tell you again... - it will give you London time

oak flare
#

but thats what i need. i just told you

weak abyss
#

There:

oak flare
#

ffs

#

nvm

weak abyss
#

I achieved with added hours to my Lithuanian time.

oak flare
#

i sorted it out

#

man reik GMT laiko, nereik man prisidet triju valandu 😄

weak abyss
#

Supratau

#

Aš pasidariau kaip aš noriu.

oak flare
#

viskas ok. aciu uz pagalba 💪