Joda DateTime to String

This is a short post that show you how to format Joda date time to String and String to Joda date time.

[googlefont font=”Courgette” fontsize=”25″]String to DateTime[/googlefont]

//... code omitted
public static void main(String[] args) {

   DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MM-yyyy");
   DateTime goodDay = fmt.parseDateTime("12-12-2013");
   System.out.println(goodDay);

}

 

[googlefont font=”Courgette” fontsize=”25″]DateTime to String[/googlefont]

//... code omitted
public static void main(String[] args) {

   DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MM-yyyy");
   DateTime now = new DateTime();
   String output = fmt.print(now);
   System.out.println(output);

}

 

[googlefont font=”Courgette” fontsize=”25″]Calendar to String[/googlefont]

//... code omitted
public static void main(String[] args) {

  Calendar cal = Calendar.getInstance();
  cal.setTime(new Date());

  SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy");
  String output = fmt.format(cal.getTime());
  System.out.println(output);

}

 

[googlefont font=”Courgette” fontsize=”25″]String to Calendar[/googlefont]

//... code omitted
public static void main(String[] args) {

  SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy");
  String strDate = "26-10-2000";
  Date date = fmt.parse(strDate);

  Calendar cal = Calendar.getInstance();
  cal.setTime(date);

}
Joda DateTime to String

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.