Posts

Showing posts from January, 2022

How Return Response From A RestService

private static String getRestCallResponse(String restURL) throws IOException { String output = ""; StringBuffer response = new StringBuffer(""); URL url = new URL(restURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); while ((output = br.readLine()) != null) { response = response.append(output); } conn.disconnect(); return response.toString(); }

Duration, DateTimeFormatter, ZonedDateTime Program

import java.time.Clock; import java.time.Duration; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class DurationClassDemo { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssZ"); String expiretime = "PT20S"; //Parse Time for 20 Seconds Duration duration = Duration.parse(expiretime); // Forms a duration object with given pass time String passedTime = formatter.format(ZonedDateTime.now(Clock.systemDefaultZone()).plus(duration)); System.out.println("Required ExpireTime = " +expiretime); System.out.println("Current Time = " + ZonedDateTime.now(Clock.systemDefaultZone())); //Print current Time System.out.println("Final Expire Time = " + passedTime); System.out.println(); System.out.println(); System.out.println(); expiretime = "PT20M"; //Parse Time for 20 Minutes duration = Du...

Using LocalDate Class

import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class StringToDate { public static void main(String[] args) { LocalDate localDate = LocalDate.now().plusDays(2); System.out.println(localDate);//TodayDate +2 => 2022-01-28 String comparingDate = localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")); System.out.println(comparingDate); //28-01-2022 } } Output : 2022-01-28 28-01-2022

How to compare dates in Java

This article shows few examples to compare two dates in Java. Updated with Java 8 examples. 1. Compare two date 1.1 Date.compareTo 1.2 Date.before(), Date.after() and Date.equals() 1.3 Check if a date is within a certain range 2. Compare two calendar 3. Compare two date and time (Java 8) 3.1 Compare two LocalDate 3.2 Compare two LocalDateTime 3.3 Compare two Instant 4 Check if a date is within a certain range (Java 8) 5 Check if the date is older than 6 months References 1. Compare two date For the legacy java.util.Date , we can use compareTo , before() , after() and equals() to compare two dates. 1.1 Date.compareTo Below example uses Date.compareTo to compare two java.util.Date in Java. Return value of 0 if the argument date is equal to the Date . Return value is greater than 0 or positive if the Date is after the argument date. Return value is less than 0 or negative if the Date is before the argument date. Review the Date.compareTo method signature. Date.java package java...

How to convert String to Date – Java

In this tutorial, we will show you how to convert a String to java.util.Date . Many Java beginners are stuck in the Date conversion, hope this summary guide will helps you in some ways. // String -> Date SimpleDateFormat.parse(String); // Date -> String SimpleDateFormat.format(date); Refer to table below for some of the common date and time patterns used in java.text.SimpleDateFormat , refer to this JavaDoc Letter Description Examples y Year 2013 M Month in year July, 07, 7 d Day in month 1-31 E Day name in week Friday, Sunday a Am/pm marker AM, PM H Hour in day 0-23 h Hour in am/pm 1-12 m Minute in hour 0-60 s Second in minute 0-60 Note You may interest at this Java 8 example – How to convert String to LocalDate 1. String = 7-Jun-2013 If 3 ‘M’, then the month is interpreted as text (Mon-Dec), else number (01-12). TestDateExample1.java package com.mkyong.date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; p...

Java Date and Calendar examples

This tutorial shows you how to work with java.util.Date and java.util.Calendar . 1. Java Date Examples Few examples to work with Date APIs. Example 1.1 – Convert Date to String. SimpleDateFormat sdf = new SimpleDateFormat ( "dd/M/yyyy" ); String date = sdf.format( new Date ()); System.out.println(date); //15/10/2013 Example 1.2 – Convert String to Date. SimpleDateFormat sdf = new SimpleDateFormat ( "dd-M-yyyy hh:mm:ss" ); String dateInString = "31-08-1982 10:20:56" ; Date date = sdf.parse(dateInString); System.out.println(date); //Tue Aug 31 10:20:56 SGT 1982 P.S Refer to this – SimpleDateFormat JavaDoc for detail date and time patterns. Example 1.3 – Get current date time SimpleDateFormat dateFormat = new SimpleDateFormat ( "yyyy/MM/dd HH:mm:ss" ); Date date = new Date (); System.out.println(dateFormat.format(date)); //2013/10/15 16:16:39 Example 1.4 – Convert Calendar to Date Calendar ...