Last active
May 10, 2017 12:06
-
-
Save GavinDmello/8cbf0a2bdf972247571bfc7093c2f0f4 to your computer and use it in GitHub Desktop.
Readable dates in java
This file contains 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
/** | |
* | |
* @author gavindmello | |
*/ | |
import java.time.*; | |
import java.sql.Timestamp; | |
import java.text.SimpleDateFormat; | |
public class JavaDate { | |
/* | |
Accepts timestamp in long, should be in milliseconds though | |
*/ | |
public String getTimeString(long timestamp) { | |
Timestamp t = new Timestamp(timestamp); | |
String timeString = t.toLocalDateTime().toLocalDate().toString(); | |
String yesterday = LocalDate.now().minusDays(1L).toString(); | |
String today = LocalDate.now().toString(); | |
if (timeString.equals(yesterday)) { | |
return "yesterday"; | |
} | |
if (timeString.equals(today)) { | |
return "today"; | |
} | |
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy, h:mm a"); | |
String date = sdf.format(timestamp); | |
return date; | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
JavaDate s = new JavaDate(); | |
System.out.println(s.getTimeString((long)1485561600*1000)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment