Merge branch 'laurentiud-master'
This commit is contained in:
		
						commit
						c903bbbe18
					
				
							
								
								
									
										17
									
								
								core-java-modules/core-java-date-operations/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								core-java-modules/core-java-date-operations/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,17 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||
|          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
|     <artifactId>core-java-date-operations</artifactId> | ||||
|     <version>${project.parent.version}</version> | ||||
|     <packaging>jar</packaging> | ||||
| 
 | ||||
|     <parent> | ||||
|         <groupId>com.baeldung</groupId> | ||||
|         <artifactId>parent-java</artifactId> | ||||
|         <version>0.0.1-SNAPSHOT</version> | ||||
|         <relativePath>../../parent-java</relativePath> | ||||
|     </parent> | ||||
| 
 | ||||
| </project> | ||||
| @ -0,0 +1,15 @@ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import java.text.ParseException; | ||||
| import java.util.Calendar; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| public class CalendarUtils { | ||||
| 
 | ||||
|     public static Calendar getPlusDays(Date date, int amount) throws ParseException { | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|         calendar.setTime(date); | ||||
|         calendar.add(Calendar.DAY_OF_YEAR, amount); | ||||
|         return calendar; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,20 @@ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import java.text.ParseException; | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| public class DateUtils { | ||||
| 
 | ||||
|     public static Date getNow() { | ||||
|         return new Date(); | ||||
|     } | ||||
| 
 | ||||
|     public static Date getDate(long millis) { | ||||
|         return new Date(millis); | ||||
|     } | ||||
| 
 | ||||
|     public static Date getDate(String dateAsString, String pattern) throws ParseException { | ||||
|         return new SimpleDateFormat(pattern).parse(dateAsString); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package com.baeldung.datetime.sql; | ||||
| 
 | ||||
| import java.sql.Date; | ||||
| import java.text.ParseException; | ||||
| import java.text.SimpleDateFormat; | ||||
| 
 | ||||
| public class DateUtils { | ||||
| 
 | ||||
|     public static Date getNow() { | ||||
|         return new Date(System.currentTimeMillis()); | ||||
|     } | ||||
| 
 | ||||
|     public static Date getDate(String dateAsString) { | ||||
|         return Date.valueOf(dateAsString); | ||||
|     } | ||||
| 
 | ||||
|     public static Date getDate(String dateAsString, String pattern) throws ParseException { | ||||
|         java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString); | ||||
|         return new Date(customUtilDate.getTime()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package com.baeldung.datetime.sql; | ||||
| 
 | ||||
| import java.sql.Time; | ||||
| import java.text.ParseException; | ||||
| import java.text.SimpleDateFormat; | ||||
| 
 | ||||
| public class TimeUtils { | ||||
| 
 | ||||
|     public static Time getNow() { | ||||
|         return new Time(System.currentTimeMillis()); | ||||
|     } | ||||
| 
 | ||||
|     public static Time getTime(String timeAsString) { | ||||
|         return Time.valueOf(timeAsString); | ||||
|     } | ||||
| 
 | ||||
|     public static Time getTime(String dateAsString, String pattern) throws ParseException { | ||||
|         java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString); | ||||
|         return new Time(customUtilDate.getTime()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package com.baeldung.datetime.sql; | ||||
| 
 | ||||
| import java.sql.Timestamp; | ||||
| import java.text.ParseException; | ||||
| import java.text.SimpleDateFormat; | ||||
| 
 | ||||
| public class TimestampUtils { | ||||
| 
 | ||||
|     public static Timestamp getNow() { | ||||
|         return new Timestamp(System.currentTimeMillis()); | ||||
|     } | ||||
| 
 | ||||
|     public static Timestamp getTimestamp(String timestampAsString) { | ||||
|         return Timestamp.valueOf(timestampAsString); | ||||
|     } | ||||
| 
 | ||||
|     public static Timestamp getTimestamp(String dateAsString, String pattern) throws ParseException { | ||||
|         java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString); | ||||
|         return new Timestamp(customUtilDate.getTime()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.baeldung.datetime.CalendarUtils; | ||||
| import com.baeldung.datetime.DateUtils; | ||||
| 
 | ||||
| import java.text.ParseException; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| public class CalendarUtilsUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenDateAndDaysToAdd_thenCalendarIsCorrectlyReturned() throws ParseException { | ||||
|         Date initialDate = DateUtils.getDate("2020/01/01", "yyyy/MM/dd"); | ||||
|         Date expectedDate= DateUtils.getDate("2020/01/11", "yyyy/MM/dd"); | ||||
|         assertEquals(expectedDate, CalendarUtils.getPlusDays(initialDate, 10).getTime()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,25 @@ | ||||
| package com.baeldung.datetime; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.baeldung.datetime.DateUtils; | ||||
| 
 | ||||
| import java.text.ParseException; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| public class DateUtilsUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenTimeMillis_thenDateIsReturned() { | ||||
|         Date now = DateUtils.getNow(); | ||||
|         assertEquals(DateUtils.getDate(now.getTime()), now); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException { | ||||
|         long milliseconds = new Date(2020 - 1900, 0, 1).getTime(); | ||||
|         assertEquals(DateUtils.getDate(milliseconds), DateUtils.getDate("2020/01/01", "yyyy/MM/dd")); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| package com.baeldung.datetime.sql; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.baeldung.datetime.sql.DateUtils; | ||||
| 
 | ||||
| import java.text.ParseException; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| public class DateUtilsUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenCurrentDate_thenTodayIsReturned() { | ||||
|         assertEquals(DateUtils.getNow(), new Date()); | ||||
|     } | ||||
| 
 | ||||
|     @Test(expected = IllegalArgumentException.class) | ||||
|     public void givenDateAsString_whenPatternIsNotRespected_thenExceptionIsThrown() { | ||||
|         DateUtils.getDate("2020 01 01"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException { | ||||
|         assertEquals(DateUtils.getDate("2020-01-01"), DateUtils.getDate("2020/01/01", "yyyy/MM/dd")); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| package com.baeldung.datetime.sql; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.baeldung.datetime.sql.TimeUtils; | ||||
| 
 | ||||
| import java.text.ParseException; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| public class TimeUtilsUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenCurrentTime_thenNowIsReturned() { | ||||
|         assertEquals(TimeUtils.getNow(), new Date()); | ||||
|     } | ||||
| 
 | ||||
|     @Test(expected = IllegalArgumentException.class) | ||||
|     public void givenTimeAsString_whenPatternIsNotRespected_thenExceptionIsThrown() { | ||||
|         TimeUtils.getTime("10 11 12"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenTimeAndPattern_thenTimeIsCorrectlyReturned() throws ParseException { | ||||
|         assertEquals(TimeUtils.getTime("10:11:12"), TimeUtils.getTime("10 11 12", "hh mm ss")); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,29 @@ | ||||
| package com.baeldung.datetime.sql; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.baeldung.datetime.sql.TimestampUtils; | ||||
| 
 | ||||
| import java.text.ParseException; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| public class TimestampUtilsUnitTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenCurrentTimestamp_thenNowIsReturned() { | ||||
|         assertEquals(TimestampUtils.getNow() | ||||
|             .getTime(), new Date().getTime()); | ||||
|     } | ||||
| 
 | ||||
|     @Test(expected = IllegalArgumentException.class) | ||||
|     public void givenTimestampAsString_whenPatternIsNotRespected_thenExceptionIsThrown() { | ||||
|         TimestampUtils.getTimestamp("2020/01/01 10:11-12"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenTimestampAndPattern_thenTimestampIsCorrectlyReturned() throws ParseException { | ||||
|         assertEquals(TimestampUtils.getTimestamp("2020-01-01 10:11:12"), TimestampUtils.getTimestamp("2020/01/01 10:11-12", "yyyy/MM/dd hh:mm-ss")); | ||||
|     } | ||||
| } | ||||
| @ -19,6 +19,7 @@ | ||||
|         <module>core-java-lang-operators</module> | ||||
|         <module>core-java-networking-2</module> | ||||
|         <module>core-java-security-manager</module> | ||||
|         <module>core-java-date-operations</module> | ||||
|     </modules> | ||||
|      | ||||
| </project> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user