Posts

Showing posts with the label HowToProgram

Java 8 – How to Format Date, LocalDate, LocalDateTime and ZonedDateTime

This quick tutorial is going to cover about format date in Java 8. We’ll get through how to format a Date, LocalDate, LocalDateTime and ZonedDateTime in Java 8.1. Format A Date Object in Java 8A quick way to format a java.util.Date object in Java is to use the SimpleDateFormat class. Let’s see some examples as follows: @Test public void testFormatDate() { //create a date object by using Calendar Calendar calendar = new GregorianCalendar(2020, Calendar.AUGUST, 9); Date date = calendar.getTime(); //create a pattern String pattern = "dd/MM/yyyy"; SimpleDateFormat dateFormatter = new SimpleDateFormat(pattern); // format the date String olympicDate = dateFormatter.format(date); //Validate assertEquals("09/08/2020", olympicDate); } Below are some examples of patterns:PatternsExamplesyyyy/MM/dd2020/08/09yyyy/MM/dd HH:mm:ss2020/08/09 23:59:59yyyy/MM/dd HH:mm:ss.SSS2020/08/09 23:59:59.000EEEEE MMMMM yyyy HH:mm:ss.SSSSunday Aug…

Java 8 – Convert LocalDate and LocalDateTime to Date and Vice Versa

This quick tutorial is going to cover how to convert LocalDate and LocalDateTime to Date and vice versa in Java 8. And note that the LocalDate, LocalDateTime and Instant classes used in this tutorial are new data types introduced from Java 8.1. Convert LocalDate to Date in Java 8To convert a LocalDate object to a java.util.Date object in Java 8, we can use the syntax as follows: Date date = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); Note that we are using the default zone which should be changed to your desired ones.Let’s see the following example which we convert a LocalDate object to a java.util.Date object. @Test public void testConvertLocalDateToDate() { LocalDate localDate = LocalDate.of(2017, 8, 10); Date date = Date.from(localDate.atStartOfDay().atZone(ZoneId .systemDefault()).toInstant()); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); assertEquals(localDate.getYear(), calendar.get(…

Configure Basic Authentication on Unirest for Java

1. IntroductionThis short tutorial is going to cover how to configure basic authentication on Unirest for Java, a simplified, lightweight HTTP request library built and maintained by Mashape.2. Configure Basic Authentication on Unirest for Java2.1. Library dependency <dependency> <groupId>com.mashape.unirest</groupId> <artifactId>unirest-java</artifactId> <version>1.4.9</version> </dependency> The only one Maven dependency for this example is the unirest-java, version 1.4.9. There are dependencies for Unirest-Java such as Apache Httpclient, Json, we will find them included automatically in the classpath.
The sample source code and related can be found on my Github project.2.2. Basic Authentication with Native APIUnirest supports Basic Authentication natively. As as result, authenticating the request with basic authentication can be done simply by calling the basicAuth(username, password) function, for example: @Test public …

Vagrant – Copy Files and Folders From Host To Guest

Image
1. OverviewThis tutorial will show how to copy files and folders from host to guest in Vagrant.2. Copy Files and Folders from Host to Guest in Vagrant2.1. Using Synced Folders or Shared FoldersSynced folders or Shared folder enable Vagrant to sync a folder on the host machine to the guest machine, allowing you to continue working on your project’s files on your host machine but use the resources in the guest machine to compile or run your project.By default, Vagrant will share our project directory (the directory with the Vagrantfile) to /vagrant. Let’s see an example of the defaut shared folder created by Vagrant on VirtualBox:
Default Synced Folders In Vagrant To configure another synced folder, you can follow the below syntax: Vagrant.configure("2") do |config| # other config here config.vm.synced_folder "/home/data/", "/data/shared" end For more detail, you can read this basic usage page.In short, to copy files and folders from host to guest in Vag…

Where Does Vagrant Store Its Boxes After Downloading?

Image
This quick tutorial is going to cover about where does Vagrant store its boxes after downloading.1. Where does Vagrant store its boxes?By default, Vagrant stores its boxes after downloading in the directory defined by VAGRANT_HOME environment variable. As a result,On Mac OS X and Linux: ~/.vagrant.d/boxesOn Windows: C:/Users/USERNAME/.vagrant.d/boxes2. Change the directory where Vagrant stores its boxes?There are some situations that we may want to change the directory where Vagrant stores its boxes to other directories such as to backup the boxes, to reduce disk space on the main partition, etc. In such a case, we can change the VAGRANT_HOME variable to our desired location.Here is an example how we can change the VAGRANT_HOME environment to another location on Unix-like systems: export VAGRANT_HOME=/data/vagrant On Windows, we can use the “setx” command, for example: setx VAGRANT_HOME "D:\data\vagrant" Or you can:Open Control Panel –> System –> Advanced System Settin…

How To Install ActiveMQ on Ubuntu 16.04 LTS (Xenial Xerus)

Image
1. IntroductionApache ActiveMQ is a popular and powerful open source messaging and Integration Patterns server. In this guide, we’re going to install ActiveMQ on Ubuntu 16.04.2. PrerequisitesJava 1.7 or later.The JAVA_HOME environment variable is set correctly.3. Install ActiveMQ on Ubuntu 16.04 Manually3.1. Download the ActiveMQWe can download the ActiveMQ zipped tarball file from ActiveMQ website, using either a browser or a tool, i.e., wget, scp, ftp, etc. For example, the following command will download ActiveMQ version 5.15.0. http://ift.tt/2vjCSU6 3.2. Extract the files from the zipped tarball into a directory of your choice.For example, the following command will extract the downloaded file into /opt/apache-activemq-5.15.0/ sudo tar -xf apache-activemq-5.15.0-bin.tar.gz -C /opt/ 3.3. Start ActiveMQTo start ActiveMQ in foreground: sudo /opt/apache-activemq-5.15.0/bin/activemq console To start ActiveMQ in background: sudo /opt/apache-activemq-5.15.0/bin/activemq start 3.4. Verif…

Install Apache Kafka On Windows

This quick tutorial is going to cover how to install Apache Kafka on Windows.1. PrerequisitesInstall Java (The latest released version of JDK 1.8 is recommended)2. Download Apache Kafka DistributionDownload an Apache Kafka distribution from its website. The current version is 0.11.0.0. After downloading, extract it to any desired location, for example, D:\kafka_2.11-0.11.0.0. Then open Command Prompt (cmd) and go to that directory: cd D:\kafka_2.11-0.11.0.0 3. Start the server3.1. Start ZooKeeperKafka uses ZooKeeper so you will need to first start a ZooKeeper server first. You can use your own ZooKeeper or use the single-node ZooKeeper instance packaged with Apache Kafka. In this tutorial, we will use the ZooKeeper packaged with Kafka.Let’s start the ZooKeeper by running the below command: .\bin\windows\zookeeper-server-start.bat config\zookeeper.properties The successful start will output the logs to the command prompt similarly to below: INFO maxSessionTimeout set to -1 (org.apache.…

How To Pull A Docker Image And Run A Container

This quick tutorial is going to cover how to pull a Docker image and run a container.1. List or Search For a Docker ImageBefore we can pull a Docker image and run a container, we should know its name first. And to do that, we can go to the Docker hub or Docker store to search for any name.The alternative way is to use the docker search command to list or search for a Docker image. Let’s see the following example which we search for an Ubuntu docker from the default public registry: [vagrant@localhost ~]$ docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating s... 5710 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 77 [OK] ubuntu-upstart Upstart is an event-based replacement for ... 71 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC sessi... 45 …

Attach and Detach from Docker Container

1. OverviewThis tutorial is going to cover how to attach and detach from Docker container. It could be necessary either to view its ongoing output or to control it interactively and then detach from it when we’re done.2. Attach to a Running Docker ContainerTo attach to a running Docker container, we can use the docker attach command which has the syntax as the following: docker attach [OPTIONS] CONTAINER Some command’s options:Name, shorthandDefaultDescription–detach-keysTableOverride the key sequence for detaching a container–no-stdinfalseDo not attach STDIN–sig-proxytrueProxy all received signals to the process Example:We can attach to the same contained process multiple times simultaneously, screen sharing style, or quickly view the progress of our detached process.Let’s say we have an opensuse docker and start it in background as the following: docker run -d --name attachdemo opensuse /usr/bin/top -bTo attach to the above running container, we can issue the following command: [vagr…

Using New Composed Annotations for @RequestMapping in Spring Framework

1. OverviewThis article is going to cover about using new composed annotations for @RequestMapping, the annotation for mapping web requests onto specific handler classes and/or handler methods in Spring Framework. Those annotations, which include @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping, have been introduced in the Spring Framework 4.3.2. New Composed Annotations for @RequestMappingLet’s see how the new composed annotations act as shortcuts for @RequestMapping annotation in the following situations:Composed AnnotationsShortcut For@GetMapping@RequestMapping(method = RequestMethod.GET)@PostMapping@RequestMapping(method = RequestMethod.POST)@PutMapping@RequestMapping(method = RequestMethod.PUT)@DeleteMapping@RequestMapping(method = RequestMethod.DELETE)@PatchMapping@RequestMapping(method = RequestMethod.PATCH) 2.1. @GetMapping Annotation@GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET)2.2. @PostMapp…

How To Remove Docker Images, Containers, Volumes, and Networks

This tutorial is going to cover how to remove Docker images, containers, volumes, and networks.1. Remove Docker Images1.1. List All Docker ImagesBefore removing a Docker image, we have to know its name or id first. And we can use the docker images command to list all Docker images in our environment. Let’s refer to another tutorial for more basic Docker commands.To list all Docker images: docker images For example, docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 4a415e366388 5 days ago 3.98 MB hello-world latest 48b5124b2768 7 weeks ago 1.84 kB busybox latest 7968321274dc 7 weeks ago 1.11 MB iron/node latest 9ca501065d18 10 months ago 18.6 MB To list all Docker images including intermediate images: docker images -a 1.2. Remove One or More Docker Images1.2.1. Using the d…

How to Get IP Address of a Docker Container

This quick tutorial is going to cover how to get IP address of a Docker container from host machine or inside a Docker container.1. Get ID or Name of a Docker ContainerWe’re going to review how to get ID or name of a Docker container first because they’re prerequisite to get IP address of the Docker container. You can refer to another tutorial for more basic Docker commands.To get ID or name of a Docker container, we can issue the following command from the host machine: docker ps By default, the command shows all running containers as the following: CONTAINER ID IMAGE CREATED STATUS PORTS NAMES 260f108e4271 wurstmeister/kafka 2 minutes ago Up 2 minutes tender_gates 4a432e03f0c3 wurstmeister/zookeeper 3 minutes ago Up 3 minutes 22/tcp naughty_mestorf We can show all containers by adding the -a option for the command: docker ps -a We can filter by using | operator: docker ps -a | grep kafka After getting the ID or name of the D…