user19336013
user19336013

Reputation:

Figuring Out Local Time with Adding Manual Hour Method in Java

Hello I have a method which adds a time to my current time. What I am looking for is I want to add this code a local time info because doesnt get the local time in my country correctly. I searched in the stackoverflow but couldnt find a similar topic for this case. I am open your suggestions, thank you.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class MyClass {
    public static void main(String args[]) {
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.HOUR_OF_DAY, 8);
      DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
      System.out.println(dateFormat.format(cal.getTime()));
    }
}

I have changed the code with java.time utilities

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class MyClass {
    public static void main(String args[]) {
      DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

      LocalDateTime date = LocalDateTime.now();
      System.out.println(dateFormat.format(date));
      System.out.println(dateFormat.format(date.plusHours(10)));  
    }
}

Upvotes: 0

Views: 260

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338785

tl;dr

ZonedDateTime
.now( ZoneId.of( "Europe/Istanbul" ) )
.plusHours( 10 )

No, not Calendar

Never use the terrible Calendar & SimpleDateFormat legacy classes.

No, not LocalDateTime

Never call LocalDateTime.now. I cannot imagine a case where that is the right thing to do.

The LocalDateTime class lacks the context of a time zone or offset from UTC. So that class cannot represent a moment, a specific point on the timeline.

To track a moment, use: Instant, OffsetDateTime, or ZonedDateTime classes.

ZoneId

Specify your time zone.

ZoneId z = ZoneId.of( "Europe/Istanbul" ) ;

Or get the JVM‘s current default time zone.

ZoneId z = ZoneId.systemDefault() ;

ZonedDateTime

Get the current moment.

ZonedDateTime now = ZonedDateTime.now( z ) ;

Add time.

ZonedDateTime later = now.plusHours( 10 ) ;

Upvotes: 3

Sorin Penteleiciuc
Sorin Penteleiciuc

Reputation: 865

Unfortunately you cannot really use the timezone because you get it from your operating system. If the OS gives you UTC, either configure it to Turkey or change it inside the application.

Since you know your location, just do this:

 LocalDateTime date = LocalDateTime.now((ZoneId.of("Europe/Istanbul"));

This question from below might help : how can i get Calendar.getInstance() based on Turkey timezone

You can also deduce your timezone using your internet provider. Below there are 2 examples.

timezone example 1

       RestTemplate restTemplate = new RestTemplate();
       String timezone = restTemplate.getForObject("https://ipapi.co/timezone", String.class);

timezone example 2

        String timezone = restTemplate.getForObject("http://ip-api.com/line?fields=timezone", String.class);
     

After getting the timezone:

        LocalDateTime date = LocalDateTime.now(ZoneId.of(timezone));

Upvotes: 2

Related Questions