Created
April 25, 2017 16:33
-
-
Save claraj/ae9da47bb23666694880673d3b85e2e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
/** | |
* Created by clara on 4/18/17. Example Date manipulations. | |
*/ | |
public class DateOperations { | |
public static void main(String[] args) { | |
// Use SimpleDateFormat to create a String, in a particular format, from a Date object | |
// https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html | |
// Use whatever format pattern you want, examples at link above. | |
// // example: 2017_04_18_13_13_16 | |
String formatPattern = "yyyy_MM_dd_HH_mm_ss"; | |
SimpleDateFormat dateFormatter = new SimpleDateFormat(formatPattern); | |
Date date = new Date(); | |
String dateStr = dateFormatter.format(date); | |
System.out.println(dateStr); | |
// Convert a Date to a timestamp, convert a timestamp to a Date | |
// timestamp = number of milliseconds since Midnight UTC, January 1, 1970 | |
Date now = new Date(); | |
long timeStamp = now.getTime(); | |
System.out.println("The timestamp for the current date is " + timeStamp); | |
Date fromTimeStamp = new Date(timeStamp); | |
System.out.println("A new date object from a timestamp is " + fromTimeStamp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment