Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8039037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:26:25+00:00 2026-06-05T03:26:25+00:00

The problem is the SimpleDateFormat seems to be adding 17 hours to the actual

  • 0

The problem is the SimpleDateFormat seems to be adding 17 hours to the actual timestamp.

This should be something really simple. I’m not sure what I’m doing wrong. I have a method that will convert a long of nanoseconds to a formatted timestamp. It’s adding 17 hours. Here’s my SSCCE

package playground;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

/**
*
* @author kentcdodds
*/
public class NanosecondsToString {

  public static void main(String[] args) {
    long nanoseconds = 234236402;
    Timestamp ts = new Timestamp(TimeUnit.MILLISECONDS.convert(nanoseconds, TimeUnit.NANOSECONDS));
    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss:SSS");
    String formatted = format.format(ts);
    System.out.println(formatted);
  }
}

The output: 17:00:00:234

I’m in Mountain Standard Time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:26:28+00:00Added an answer on June 5, 2026 at 3:26 am

    You have to set the timezone to UTC.

    public static void main(String[] args) throws IOException {
                  long nanoseconds = 234236402;
                  Timestamp ts = new Timestamp(TimeUnit.MILLISECONDS.convert(nanoseconds, TimeUnit.NANOSECONDS));
                  SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss:SSS");
                  format.setTimeZone(TimeZone.getTimeZone("UTC"));
                  String formatted = format.format(ts);
                  System.out.println(formatted);
        }
    

    SimpleDateFormat will convert the timestamp to a time in your current time zone by default. So it is necessary to tell it to assume it as UTC. Otherwise, you can use jodatime’s interval class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this simple date format SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss.SSS Z);
I have a problem with my first android widget... I am doing like this:
I am trying to parse this date with SimpleDateFormat and it is not working:
I've been working away at this problem for the past 10-12 hours , and
I am having problem with this small piece of code SimpleDateFormat sf = new
Am having problem in using simple date format for this: having date as 12/13/2011
this problem is killing me, here is my code import java.text.ParseException; import java.text.SimpleDateFormat; import
This is probably a very simple question but I dont see the problem or
Problem: I have a list containg hours, for example: 08:15:00 08:45:00 09:00:00 12:00:00 ...
Apparently, I'm missing something fundamental. I'm having a problem with formatting the value of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.