Java SimpleDateFormat example: How to convert a Date to a formatted String
Summary: This tutorial demonstrates how to use the Java SimpleDateFormat class to convert a Java Date
to a formatted String
.
Summary: This tutorial demonstrates how to use the Java SimpleDateFormat class to convert a Java Date
to a formatted String
.
I’ve been working with the Java JOptionPane showMessageDialog
a lot lately, so I thought I’d create a page here with a number of showMessageDialog
examples, sort of a JOptionPane reference page.
I’ll walk you through some JOptionPane examples here, starting with a simple example and then increasing the level of difficulty as I go on.
Java FAQ: How do I format Java double
and float
output to two decimal places, such as when I want to format its output?
There are at least two ways to round a double
or float
value to two decimal places in Java:
Java FAQ: How do I initialize/populate a static List
(ArrayList
, LinkedList
) in Java?
Java array FAQ: How do you create an array of Java int
values (i.e., a Java “int array”)?
Answer: There are several ways to define an int array in Java; let’s take a look at a few examples.
If you know the desired size of your array, an you'll be adding elements to your array some time later in your code, you can define a Java int array using this syntax:
It's been so long since I create a two-dimensional Java array in I almost couldn't remember how to do it. Fortunately I did, and I thought I'd include my sample code here in case it will help anyone else.
So, in this quick tutorial, I'll show you how to create a two-dimensional array (2D array) of Java String
objects, and then I'll show you how to access each element in the array.
First, here's how I create the two-dimensional arrays of strings with this code:
While working with various "Java instanceof" tests recently, my curiosity was piqued, and I thought I'd take a look at how the instanceof operator works when testing against a Java array.
Scala FAQ: How do I use the Scala varargs syntax (or, What is the Scala varargs syntax)?
You can define a Scala function that accepts a varargs parameter like this:
def printAll(strings: String*) { strings.map(println) }
The only magic in using the varargs syntax is adding the *
symbol after the String declaration, as highlighted in this line of code in the function declaration:
Java file writing FAQ: How do I append text to the end of a text file in Java?
The short answer is that you should create a FileWriter
instance with the append flag set to true, like this:
BufferedWriter bw = new BufferedWriter(new FileWriter("checkbook.dat", true));
The rest of this article explains this.
Summary: This is a Java/MySQL SQL SELECT example, demonstrating how to issue a SQL SELECT command from your Java source code, using a MySQL database.
[toc]
Back when I was interviewing for computer programming positions in Boulder and Louisville, Colorado, I found that many interviewers ask questions about Java serialization. After being asked about serialization for the third time, I remembered an old Java deep clone hack that takes advantage of serialization.
[toc]
Java enumerations FAQ: Can you share some Java enum examples, such as how to declare a Java enum, and how to use a Java enum in a for loop, if/then statement, and Java switch statement?
Sure. As described in the Sun/Oracle Java documentation, “you should use enum types any time you need to represent a fixed set of constants.” Let's take a look at some enum examples to see how this works.
Java array FAQ: Can you share an example of how to sort a Java String
array?
Sure. The following source code shows how to sort an array of strings. In short, you can easily sort a string array with the Arrays class sort
method, shown in bold below:
[toc]
Java/Scala memory FAQ: How do I control the amount of memory my Java program uses (i.e., Java RAM usage)?
The short answer is that you use these java
command-line parameters to help control the RAM use of your application:
[toc]
Java math FAQ: How do I square a number in Java?
You can square a number in Java in at least two different ways:
Math.pow
functionSummary: This page is a printf
formatting cheat sheet or reference page. I originally created this printf
cheat sheet for my own programming purposes, and then thought it might be helpful to share it here.
A great thing about the printf
formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Python, Perl, PHP, Ruby, Scala, and others. This means that your printf
knowledge is reusable, which is a good thing.
Java jar file reading FAQ: Can you show me how a Java application can read a file from own of its own Jar files?
Here's an example of some Java code I'm using to read a file (a text file) from a Java Jar file. This is useful any time you pack files and other resources into Jar files to distribute your Java application.
The source code to read a file from a Java Jar file uses the getClass and getResourceAsStream methods:
I thought I'd share the source code for my Java email address validator class. I'm not sure if there's a big need for it ... I wrote it a long time ago, and I think I created it because Java's javax.mail.internet.InternetAddress
class wasn't validating email addresses as deeply as I wanted it to. For instance, I think it would allow the string "fred" to be a valid email address, but on the internet you really want to see something like "fred@foo.bar". So I think that's where this class comes from.
Summary: This article shares the source code for a simple Java Socket client class. This article demonstrates both (a) how to write to a Java socket, and (b) how to read from a Java socket.