0

I am trying to give timestamp to the file name, but it is giving epoch time, is there anyway to do that?

import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
import java.time.*;

File scrFile = ((TakesScreenshot)control).getScreenshotAs(OutputType.FILE);
String filename =  new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z'.png'").format(new Date(0)); 
File dest = new File("C:\\tmp\\" + filename);
FileUtils.copyFile(scrFile, dest);
System.out.println("The screenshot is taken");

thanks in advance.

  • 1
    You may use LocalDateTime : https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html – mike Jun 26 '20 at 04:01
  • If you are a double-click sort of person, it is better not to leave spaces in the filename otherwise when the name is clicked on, only a section of the name will be selected. – cup Jun 26 '20 at 05:35
  • Those colons in the filename won't work in Windows. – MC Emperor Jun 26 '20 at 05:48
  • 1
    Besides, you should immediately drop the usage of `Date` and `SimpleDateFormat`, and start using the classes in the `java.time` package. `DateTimeFormatter` and `LocalDateTime` may be useful in your case. – MC Emperor Jun 26 '20 at 05:50
  • java.time.Instant is a great alternative for getting epoch time stamp. It is tied to UTC so it is great for database time stamps and other records. If you want Unix epoch time in milli seconds then use 'long epochMillis = Instant.now().toEpochMilli();' Read more about java-time here: https://stackoverflow.com/questions/32437550/whats-the-difference-between-instant-and-localdatetime – DigitShifter Sep 03 '20 at 16:37

2 Answers2

0

new Date(0) initialises a Date object with 0 milliseconds after the epoch, so yes, is the epoch time.

If you want the current time you should use new Date() (without any arguments).

assylias
  • 321,522
  • 82
  • 660
  • 783
  • String filename = new SimpleDateFormat("yyyyMMddhhmmss'.png'").format(new Date()); is giving error. Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Date() is undefined its not working, however If I add Date(500000000) then its giving some other date. – Deepak Jindal Jun 26 '20 at 05:56
  • It's because you are importing `java.sql.Date` instead of `java.util.Date`. – assylias Jun 26 '20 at 06:25
  • you saved my assilias. thank you. now can you elaborate what exactly happened here? – Deepak Jindal Jun 26 '20 at 06:49
  • These are 2 different classes and the sql version doesn't have a no-arg constructor, so you need to provide a timestamp. Also, as explained in the main comments, you should use the java.time package if you can, the Date class is obsolete and should only be used when you don't have a choice (older java version or legaccy libraries).. – assylias Jun 26 '20 at 06:52
0

System.currentTimeMillis()

Use it, at any point in time, to get the number of milliseconds from January 1, 1970 UTC.

Using it, your code will be as follows:

String filename = String.valueOf(System.currentTimeMillis()) + ".png";
File dest = new File("C:\\tmp\\" + filename);
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110