1

How can I re-write the code below (that is written in C#), in java to get same result?

TimeSpan Now = DateTime.UtcNow.Subtract(new DateTime(1970,1, 1));
long TimeStamp = Convert.ToInt64(Math.Floor(Now.TotalMilliseconds));

I want to translate the C# code to java language to get the value of TimeStamp (long variable).

6
  • If you are using java8, use LocalDateTime Commented May 21, 2016 at 5:37
  • I use LocalDateTime and get time between now and 1.1.1970 by period.between. but I want to convert this to TimeStamp (long variable) format like c# code.@aviad Commented May 21, 2016 at 5:50
  • Is the above code just giving you the number of milliseconds since 1/1/1970? I'm not a C# person. What would the output of your code look like in C#? Commented May 21, 2016 at 5:52
  • @ Michael Markidis yes. It gives a number like this : 1463568180761. Commented May 21, 2016 at 5:56
  • If you are always doing it from 1/1/1970, then you can use System.currentTimeMillis(); Commented May 21, 2016 at 5:57

2 Answers 2

1

Just use:

long timestamp = System.currentTimeMillis();
Sign up to request clarification or add additional context in comments.

Comments

1

you can use this :

 java.util.Date date= new java.util.Date();
  System.out.println(new Timestamp(date.getTime()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.