Joda vs SimpleDateFormat

Java developers who are used to Joda date time library has surprises while switching to the date library provided by Java. Here are few things to be aware while using SimpleDateFormat.

SimpleDateFormat is not as strict as a Joda date formatter. While parsing a string as a date given a date pattern Joda expects the date to be valid (E.g: 31-Feb-2017 is an invalid date), but by default, SimpleDateFormat can parse invalid dates defaulting to some values. However, the parsing can be made strict by using setLinient() method. When this flag is set to false, the parsing becomes strict thereby return null dates when parsing invalid dates.

Reference:https://stackoverflow.com/questions/15336200/why-does-simpledateformat-parse-incorrect-date

Even when the setLinient is set to false, the parser will parse irrelevant characters after the length of the required date format.

Reference:https://stackoverflow.com/questions/32777439/force-simpledateformat-to-parse-the-whole-string

Please refer to the below example.


import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class SimpleDateFormatTest {

	public static void main(String[] args) {

		/*Linient Parsing*/
		final SimpleDateFormat formatterLinient = new SimpleDateFormat("M/d/yyyy", new Locale("en"));
		formatterLinient.setTimeZone(TimeZone.getTimeZone("UTC"));
		formatterLinient.applyPattern("M/d/yyyy");
		final ParsePosition posLinient = new ParsePosition(0);
		Date dateLinient = formatterLinient.parse("02/31/2016", posLinient);
		System.out.println(dateLinient); /* OUTPUT: Wed Mar 02 05:30:00 IST 2016 */

		/*Strict Parsing*/
		final SimpleDateFormat formatter = new SimpleDateFormat("M/d/yyyy", new Locale("en"));
		formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
		formatter.applyPattern("M/d/yyyy");
		formatter.setLenient(false);
		final ParsePosition pos = new ParsePosition(0);
		Date date = formatter.parse("31/02/2016", pos); //Passing an Invalid Date
		System.out.println(date); /*OUTPUT: null */

		/*Ignoring characters after the required format*/
		final SimpleDateFormat formatterJunkchars = new SimpleDateFormat("M/d/yyyy", new Locale("en"));
		formatterJunkchars.setTimeZone(TimeZone.getTimeZone("UTC"));
		formatterJunkchars.applyPattern("M/d/yyyy");
		formatterJunkchars.setLenient(false);
		final ParsePosition posJunkchars = new ParsePosition(0);
		Date dateposJunkchars = formatter.parse("02/03/2016JunkChars", posJunkchars);
		System.out.println(dateposJunkchars);/* OUTPUT: Wed Feb 03 05:30:00 IST 2016*/

	}
}

Advertisements

அன்பு மிகுந்தாலும்!

வரமா சாபமா 
தெரியலடி 
நீ என் 
வாழ்க்கையில
வந்தது.
 
என்ன நல்ல 
பாத்துக்குறேன்னு 
என் வேலையெல்லாம் 
நீ செய்ய, 
ஒன்னும் தெரியாதவ
ஆனேனடி.
 
எல்லா வேலையும் 
நீ செஞ்சும் 
உன்ன பாராட்ட 
முடியல..
சொல்லி காமிச்சடி.
 
என் வேலைய
நா செஞ்சாலும் 
உனக்கு புடிச்சமாதிரி
செய்ய சொல்லி 
கெடுத்தாயடி.
 
அக்கறைங்கிற
பேருல
என் சுதந்திரத்தை 
பறிச்சாயடி.
 
நீ செய்யறது 
தப்புனாலும் 
கோச்சிக்குவ.
உன்ன ஒன்னு 
சொல்லலனாலும் 
கோச்சிக்குவ.
 
நீ 
நல்லவளா
கட்டவளா 
தெரியலடி…
என்ன பாத்துக்குறேனு
உன்ன வருத்திக்காதடி!

Fish Curry

Healthiest form of fish is to eat it is boiled in gravy…!

Ingredients:

Fish – 1 Kg (Sliced)
Oil – 100 ml
Mustard – 1 teaspoon
Fenugreek – 1/2 teaspoon
Asafoetida powder – a pinch
Curry Leaves – hand full
Pepper – 10 seeds
Green Chilly – 4
Shallots (small onions) – 300 grams
Garlic – 10 cloves
Tomatoes – 4
Turmeric Powder – 1/2 teaspoon
Chilli Powder – as per taste
Coriander Powder – 3 teaspoon
Tamarind – 2 lemon size
Coconut – half
Corriander Leaves – 3 stems
Salt – as per taste

Steps:

1. Soak the tamarind and extract the pulp. Keep it aside.
2. Grate the coconut and grind it into a fine paste.
3. Heat oil in big pan (Big enough for the fish to be able to cook properly)
4. Temper with mustard, fenugreek, asafoetida, pepper seeds and curry leaves.
5. Add chopped green chilly.
5. Add the chopped shallots and fry until it turns golden brown.
6. Add finely cut garlic and fry it until half cooked.
7. Add finely chopped tomatoes and fry until it becomes soft. (Add a pinch of salt for the tomato to get cooked fast)
8. Add turmeric powder, chilli powder (as per taste) and coriander powder. Mix well.
9. Add the tamarind pulp and allow it to boil for 5 to 10 mins.
10. Add the coconut paste and add water as required.
11. Close the pan with a lid and Allow it to boil until the oil floats on top.
12. Now add the properly cleaned fish pieces into the boiling gravy.
13. Immediately add chopped coriander leaves.
14. Turn off the flame after 5 mins as the fish cooks really fast. Be sure not to overcook.
15. Keep the gravy aside for an hour for the fish flavour to get into the gravy.

Gravy is ready to be served with hot steamed rice!

The “Friend” who never hurts you…

Writing is always my passion. It enables me to be myself and most of the times it reflects me.

Rewinding my memories, I realise that I developed a passion for writing from my Dad. I am grateful to him for inculcating the habit in himself which is a great source of inspiration for me. Moreover, I started all this by journaling my daily activities which slowly built crave for writing.

Although I have stopped journaling every day, I have a dairy for myself to keep note of important moments. It is really refreshing when I look back those pleasant memories. However it is after all life, there are also sour moments which remind me to be strong and not repeat my mistakes.

I make my dairy colourful by pinning cards, dry flowers, etc. which I feel is a priceless possession.

My diary is also my anthology. I make note of all my favourite quotes and excerpts from the books I read which I feel guides me in my life.

The Diary, will always be your best friend, who you can go back to for anything and the best part is it NEVER HURTS YOU!

P.S: The diary in the header image is mine 🙂

கற்பனையில் மட்டும் தாய்

தாயின் பாசத்திற்கும்
பாட்டியின் பாசத்திற்கும்
இடையில் சிக்கி
தவித்தது
ஓர் குழந்தை!

தாய்
தன் குழந்தையின்
பாசத்திற்கு ஏங்கிய போது…
பாட்டி
தன் உலகமே
அந்த குழந்தை
என்றிந்தாள்…

தன் குழந்தையின் நலம்..
தன் குடும்பத்தின் நிம்மதி…
கருதி
விட்டுக்கொடுத்தாள்
தாய்மை உணர்வை…
கட்டுபடுத்திக் கொண்டாள்
ஏக்கத்தை..
அந்த தாய்..

குழந்தையை
மார்போடு அனைத்துக் கொள்ளாமல்
தூரம் நின்று ரசித்து சிரித்தாள்..
கற்பனையில் வாழ்ந்தாள்..
தன்னையே ஏம்மாற்றி கொள்வதை
அறியாத
அந்த தாய்…

Photo Courtesy: Internet