Merge branch 'master' of https://github.com/sanketmeghani/tutorials into sanketmeghani-master

This commit is contained in:
Zeger Hendrikse 2016-09-27 20:34:33 +02:00
commit ec19c55a5a
4 changed files with 47 additions and 54 deletions

View File

@ -1,20 +0,0 @@
package com.baeldung.util;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class GetCurrentDate {
public static void main(String args[]) {
LocalDate localDate = LocalDate.now();
System.out.println("Today's date is: " + localDate);
localDate = LocalDate.now(ZoneId.of("GMT+02:30"));
System.out.println("Current date in GMT +02:30 timezone: " + localDate);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Today's date is: " + localDateTime.toLocalDate());
}
}

View File

@ -1,20 +0,0 @@
package com.baeldung.util;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
public class GetCurrentTime {
public static void main(String args[]) {
LocalTime localTime = LocalTime.now();
System.out.println("Current time is: " + localTime);
localTime = LocalTime.now(ZoneId.of("GMT+02:30"));
System.out.println("Current time in GMT +02:30 timezone: " + localTime);
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current time is: " + localDateTime.toLocalTime());
}
}

View File

@ -1,14 +0,0 @@
package com.baeldung.util;
import java.time.Instant;
public class GetCurrentTimestamp {
public static void main(String args[]) {
Instant instant = Instant.now();
System.out.println("Current timestamp is: " + instant.toEpochMilli());
System.out.println("Number of seconds: " + instant.getEpochSecond());
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.util;
import static org.junit.Assert.assertEquals;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.junit.Test;
public class CurrentDateTimeTest {
@Test
public void shouldReturnCurrentDate() {
final LocalDate now = LocalDate.now();
final Calendar calendar = GregorianCalendar.getInstance();
assertEquals("10-10-2010".length(), now.toString().length());
assertEquals(calendar.get(Calendar.DATE), now.get(ChronoField.DAY_OF_MONTH));
assertEquals(calendar.get(Calendar.MONTH), now.get(ChronoField.MONTH_OF_YEAR) - 1);
assertEquals(calendar.get(Calendar.YEAR), now.get(ChronoField.YEAR));
}
@Test
public void shouldReturnCurrentTime() {
final LocalTime now = LocalTime.now();
final Calendar calendar = GregorianCalendar.getInstance();
assertEquals(calendar.get(Calendar.HOUR_OF_DAY), now.get(ChronoField.HOUR_OF_DAY));
assertEquals(calendar.get(Calendar.MINUTE), now.get(ChronoField.MINUTE_OF_HOUR));
assertEquals(calendar.get(Calendar.SECOND), now.get(ChronoField.SECOND_OF_MINUTE));
}
@Test
public void shouldReturnCurrentTimestamp() {
final Instant now = Instant.now();
final Calendar calendar = GregorianCalendar.getInstance();
assertEquals(calendar.getTimeInMillis() / 1000, now.getEpochSecond());
}
}