Date and Time Parsing and Formatting in Java with Joda Time
Posted 2008-12-04 in Java by Johann.
Joda Time is above all a replacement for the java.text.DateFormat
, java.text.SimpleDateFormat
and java.util.Calendar
classes. The original java.text
and java.util
classes are probably best known for not being thread-safe. In addition to being threadsafe, Joda Time also adds a richer and easier-to-use API.
This article is meant to be a cheat sheet for Joda Time – I will add more tips over time.
Date Parsing
The first line parses the Apache/lighttpd common or extended log file date format. The second one parses an ISO 8601 format.
DateTimeFormatter parser1 = DateTimeFormat.forPattern("dd/MMM/yyyy:HH:mm:ss Z"); DateTimeFormatter parser2 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm"); DateTime time = parser1.parseDateTime("<data>");
Time Addition and Subtraction
Do something if a date is less than one month in the past from now.
DateTime time = getTimeFromSomewhere(); DateTime now = new DateTime(); if (time.plusMonths(1).isAfter(now)) { // something interesting happens here }
All methods you’d expect are there: plusDays
, plusHours
, plusMillis
, plusMinutes
, plusMonths
, plusSeconds
, plusWeeks
, plusYears and minusDays
, minusHours
, minusMillis
, minusMinutes
, minusMonths
, minusSeconds
, minusWeeks
and minusYears
. The API is chainable/fluent, so time.plusMonths(1).plusDays(1)
works.
Before/After
Easily done using isBefore
and isAfter
:
if (time.isAfter(agent.lastSeen)) { // computer reboots here }
The X-FORWARDED-FOR HTTP header
Posted 2008-12-02 in Java by Johann.
X-FORWARDED-FOR
is a HTTP header that is inserted by proxies to identify the IP address of the client. It can also be added to requests if application servers are proxied by proxy servers. In this case, the request IP address is always a local address and the client IP address must be extracted from the request.
Since proxies can be chained – for example if the client’s request is already made through a proxy – the X-FORWARDED-FOR
header can contain more than one IP address, separated by commas. In this case, the first one should be used.
Java Code
The following Java code extracts the originating IP address of an HttpServletRequest
object.
public final class HTTPUtils { private static final String HEADER_X_FORWARDED_FOR = "X-FORWARDED-FOR"; public static String remoteAddr(HttpServletRequest request) { String remoteAddr = request.getRemoteAddr(); String x; if ((x = request.getHeader(HEADER_X_FORWARDED_FOR)) != null) { remoteAddr = x; int idx = remoteAddr.indexOf(','); if (idx > -1) { remoteAddr = remoteAddr.substring(0, idx); } } return remoteAddr; } }
JSPs
In a JSP, the X-FORWARDED-FOR
header can be retrieved as follows:
<%= request.getHeader("X-FORWARDED-FOR") %>
Of course, a Servlet Filter could replace the original HttpServletRequest
with a wrapped version that returns the X-FORWARDED-FOR
value.
Example Request
Here is a full request that was made from 129.78.138.66 through the proxy at 129.78.64.103:
2008-12-01 16:00:59,878 INFO AntiScrape - 129.78.138.66, 129.78.64.103: USER-AGENT: … HOST: johannburkard.de PRAGMA: no-cache ACCEPT: */* ACCEPT-ENCODING: identity VIA: 1.1 www-cacheF.usyd.edu.au:8080 (squid/2.6.STABLE5) X-FORWARDED-FOR: 129.78.138.66, 129.78.64.103 CACHE-CONTROL: no-cache, max-age=604800 X-HOST: johannburkard.de X-FORWARDED-PROTO: http
6 comments
Permanent and Temporary Redirects in Java (Servlet/JSP)
Posted 2008-11-08 in Java by Johann.
Redirects in Servlets
To perform a permanent or temporary redirect in a Servlet, set the status property of the HttpServletResponse
object to either SC_MOVED_PERMANENTLY
(301) or SC_MOVED_TEMPORARILY
(302) and set the Location
header to the target URL.
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader("Location", "https://johannburkard.de/");
While the HttpServletResponse
object already has a sendRedirect
method, the specification says that it sends…
…a temporary redirect response to the client using the specified redirect location URL.
This is why I select the HTTP status code manually.
Redirects in JSPs
JSPs obviously take the same code as Servlets. Here is a sample redirection in a JSP.
<%@ page import="javax.servlet.http.*"><% response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader("Location", "https://johannburkard.de/"); %>
Redirects using Refresh header
Some web analytics packages do not analyze URL parameters if the HTTP status code is not 200
. In this scenario, a redirect using the Refresh
header can also be used.
String target = "https://johannburkard.de"; response.setContentType("text/html"); response.setContentLength(13); response.addHeader("Refresh", new StringBuilder(target.length() + 5).append("0;url="). append(target).toString()); ServletOutputStream out = response.getOutputStream(); out.print("<html></html>");
This code is taken from my ReDirector servlet which uses XML files for redirection targets.
Debugging Redirection
wget is the Swiss army knife of HTTP tools and I frequently use its debug mode (-d
) to solve HTTP problems.
> wget -d http://127.0.0.1:8081/servlet/com.eaio.TestServlet DEBUG output created by Wget 1.10.2 on Windows. --10:15:54-- http://127.0.0.1:8081/servlet/com.eaio.TestServlet => `com.eaio.TestServlet' Connecting to 127.0.0.1:8081... seconds 0.00, connected. Created socket 1952. Releasing 0x003a49a8 (new refcount 0). Deleting unused 0x003a49a8. ---request begin--- GET /servlet/com.eaio.TestServlet HTTP/1.0 User-Agent: Wget/1.10.2 Accept: */* Host: 127.0.0.1:8081 Connection: Keep-Alive ---request end--- HTTP request sent, awaiting response... ---response begin--- HTTP/1.1 301 Moved Permanently Date: Sat, 08 Nov 2008 09:15:56 GMT Server: Orion/2.0.7 Connection: Close Content-Type: text/plain Location: https://johannburkard.de/ ---response end---
Backup Twitter Tweets with TwitterBackup
Posted 2008-10-16 in Java by Johann.
Are you worried about losing your Twitter tweets? I too looked for a tool to back up my Twitter updates but couldn’t find one. So – I wrote one. And you can have it for free!
TwitterBackup
TwitterBackup is a tool that downloads all your tweets and stores them in XML format. The document type is identical to Twitter’s API.
Download
Download TwitterBackup 3.2.9 (779 KB) (last updated 2016-11-06)
Source code also available (6.4 MB).
After downloading, double-click the twitterbackup-3.2.9.jar
file. If this doesn’t work, open a command prompt and type java -jar twitterbackup-3.2.9.jar
.
Running TwitterBackup
This is TwitterBackup. Fill out the fields and press “Start.”
Username
Enter your Twitter user name here. Not your email address.
Password
Well, your password.
File Name
This is the name of the file where your tweets will be stored after they have been downloaded.
If the file exists, TwitterBackup will read it and add only newer tweets to it. This is an incremental backup and faster than a full backup.
Proxy
If you are connected to the internet through a proxy, enter the proxy address and the port (optional) here. Examples: blaproxy
, blaproxy:9080
.
Start
To download all Tweets, click this button. This will start the Twitter back up process.
Notes
- TwitterBackup might not work if you have blank characters in your password.
- License is MIT License (OSI certified).
- There is a delay time of several seconds between requests (as demanded by the Twitter API).
- Depending on the number of your updates, the backup process can take several minutes.
- Your settings are stored on your computer, not on my server or anywhere on the net.
20 comments
Pages
Page 2 · Page 3 · Page 4 · Page 5 · Page 6 · Page 7 · Page 8 · Next Page »
Subscribe
RSS 2.0, Atom or subscribe by Email.
Top Posts
- DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
- 6 fast jQuery Tips: More basic Snippets
- xslt.js version 3.2 released
- xslt.js version 3.0 released XML XSLT now with jQuery plugin
- Forum Scanners - prevent forum abuse
- Automate JavaScript compression with YUI Compressor and /packer/