Formatting dates in Java with DateFormat

By dave on March 22, 2015

There are several ways to format dates in Java, but by far the easiest is to use DateFormat. Creating a DateFormat is very similar to NumberFormat that we saw on the previous page. Here are the static factory methods called directly on the DateFormat class: getDateInstance(..) getDateTimeInstance(..) getTimeInstance(..) There are several overloaded versions of each method above. We will not cover Locale here as that will be covered in another article on java timezone support.

Setting up a database realm for role based security

By dave on March 22, 2015

Following on from Setting up role based security in tomcat, we now switch from using a memory realm to one backed by a database. Memory realms are great for testing but in any real application is would probably not be acceptable. Normally user credentials are stored in a database, so for this purpose there is a realm based on a datasource. Depending on your view of things, you will either edit server.

Simple uses of CountDownLatch

By dave on March 22, 2015

CountDownLatch provides a means of waiting for a number of asynchronous events before proceeding. In order to do this one constructs a latch providing the event count. Then one thread would normally call await Whilst the other thread calls countDown. Once the count reaches zero the await call returns and the latch is set. If the call to await happens after the latch is set it returns immediately. In our example we need to wait for a thread to initialise before proceeding.

The background, string formatting in java

By dave on March 22, 2015

Over the years there have been no shortage of ways to format a string in java. What with the + operator, StringBuffer, StringBuilder, String.format(..) and various specialised formatters for numbers and dates we sometimes feel a little spoilt for choice. But how do they all work and what are their advantanges / disadvantages?  StringBuffer - a hang up from times gone by! StringBuffer is a synchronized object! Yes, everything you do with it will cause synchronization.

Using tomcat role based security in JSP"s or servlets

By dave on March 22, 2015

Following on from Setting up role based security in tomcat we now look at accessing the realm security information from code. Although tomcat takes care of authenticating users at the right time, there are still times when we need to programatically access the credential information. For example the following snippet from userProfile.jsp is a mixed mode page In that anyone can view the page, but some users with manager role see more information.

Using Java"s XMLStreamReader PULL XML Parser

By dave on February 17, 2014

In this entry I show how to use the inbuilt Java XMLStreamReader PULL parser class to read an XML file. The XML stream libraries are PULL based XML parsers that do not load the whole document into a memory structure, so therefore are more suited to large volumes of XML. Below is an example XML file for a zoo, it contains Animal data types that have both attributes and data. It is kept simple for the sake of example.

Groovychart Bar chart with fixed colour

By dave on November 30, 2013

In this article I show an example of creating a bar chart with a fixed colour. This was missing from the groovychart example set, and will get included into the next build. If you are unfamiliar with groovy chart then here is an introduction to Groovychart We simply tell the plot’s renderer to use the StandardBarPainter. This turns off the gradient paint that is used by default. Also this example shows one way of using multiple colours in the bars, its probably not the only way and as recommended elsewhere, I suggest the JFreeChart book for anyone intending serious usage of the library.

Using Semaphore and CyclicBarrier in Java applications

By dave on November 10, 2013

In this example I show an usage of two more concurrent classes CyclicBarrier and Semaphore; which are both provided in the core Java library. There's a wealth of concurrency classes built directly into the JVM that can really simplify multi-threaded development. CyclicBarrrier to make threads wait for alignment. In the example below, I use a cyclic barrier to make several threads wait for alignment. This is a common example where we have many threads and need to wait for all threads to reach a barrier before proceeding:

BlockingQueue producer consumer example

By dave on November 1, 2013

In this BlockingQueue example we show how to write a very simple producer - consumer with a blocking queue. This example generates SimpleAddition objects that require an addition of two numbers to be performed on the consumer thread. In this case the two values to be added are generated using java.util.Random's nextInt call. They are stored on the queue as a SimpleAddition transfer object and picked up for processing on the consumer thread.

NIO File operations in Java 7

By dave on October 23, 2013

Hot on the heals of the last article, interested in what other goodies may be in the new file IO package, and wanting to try the new catch block, I cooked up another example. In this example, I create a Path object for an example directory, into which I then create a file and write some text into the file. Some things of note here: We do not need to concern ourselves with closing the writer objects as they are opened in the new auto-close section.

We use cookies to analyse traffic and to personalise content. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Please see our privacy policy should you need more information or wish to adjust your settings.

Send a message
X

This message will be securely transmitted to Nutricherry LTD servers.