Read time from internet time server - working java code

Read time from internet time server - working java code
import java.io.*;
import java.net.*;
import java.util.*;

/**
 * This program makes a socket connection to the atomic clock in Boulder,
 * Colorado, and prints the time that the server sends.
 */
public class Time_Server_Socket_Test_Java {
    public static void main(String[] args) {
        try {
            Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
            try {
                InputStream inStream = s.getInputStream();
                Scanner in = new Scanner(inStream);

                while (in.hasNextLine()) {
                    String line = in.nextLine();
                    System.out.println(line);
                }
            } finally {
                s.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



iterate through map hashmap linkedhashmap in java

How to iterate through Map.. MashMap, LinkedHashMap ...  in Java
Method 1 : If you're only interested in the keys, you can iterate through the keySet() of the map:
Map map = ...;
for (String key : map.keySet()) {
    // ...
}

Method 2 :

java escape text in regular expression

Java's built‑in Pattern.quote() method lets you escape arbitrary text so it can be safely used in a regular expression.
For example, if a user enters "$5", you can match that exact string. Without escaping, the dollar sign would be interpreted as an end‑of‑line anchor. Use Pattern.quote() and the method wraps the text in \Q...\E quotes, ensuring every character is taken literally.


Here’s the one-liner :
Pattern.quote("$5");

use of @override annotation in java - why ?

The @Override annotation is your compile‑time guardian—it tells the compiler “I intend to override a parent method.” If there’s no matching superclass method, the compiler throws an error. See this example:

import java.util.HashSet;
import java.util.Set;
/**
 * Override_Annotation_Usage_Example, from  Effective Java
 */