skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Showing posts with label Date Example. Show all posts
Showing posts with label Date Example. Show all posts
Sunday, October 14, 2012

MM_DD_YYYY Date Formatter in Java

Posted by Raju Gupta at 12:00 AM – 0 comments
 
This will formte the Date to the MM-DD-YYYY format

 public static String formatDate(Date date) {
//create new calendar instance
  Calendar cal1 = Calendar.getInstance();
  String dob = "";
//check for null
  if(date==null)
//if null create new date instance
   date = new Date();
   cal1.setTime(date);
   if (cal1 != null) {
    dob = "" + ((cal1.get(Calendar.MONTH)) + 1);
    dob += "-" + cal1.get(Calendar.DATE);
    dob += "-" + cal1.get(Calendar.YEAR);
   }
 
  return dob;
 }


[ Read More ]
Read more...
Friday, September 14, 2012

Convert DateTime Using Java

Posted by Admin at 1:41 PM – 0 comments
 

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.util.TimeZone;


public class ConvertDateTime {
 
 public static TimeZone tzGMT = TimeZone.getTimeZone("GMT0");
 public static TimeZone tzEL = TimeZone.getTimeZone("Europe/London");
 
 /*
                       *converts the system time to GMT
                      */
 public static Timestamp currentTimeInGMT() {
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date date = new Date();
      DateFormat firstFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      TimeZone firstTime = TimeZone.getTimeZone(dateFormat.format(date));
      firstFormat.setTimeZone(firstTime);
      //secondFormat.setTimeZone(secondTime);
      System.out.println("Current date and time: "+dateFormat.format(date));
      System.out.println("Time in GMT: ");
                                                                   // System.out.println("Time in GMT: "+ firstFormat.format(date)   +" GMT");
   return Timestamp.valueOf(firstFormat.format(date));
      }
                     /*
                       *converts the system time to BST
                      */

 public static Timestamp currentTimeInBST() {
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date date = new Date();
      DateFormat firstFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      TimeZone firstTime = TimeZone.getTimeZone(dateFormat.format(date));
      TimeZone secondTime = tzEL;
      firstFormat.setTimeZone(firstTime);
      secondFormat.setTimeZone(secondTime);
      System.out.println("Current date and time: "+dateFormat.format(date));
      System.out.println("Time in GMT: " + firstFormat.format(date)+" GMT");
      System.out.println("Time in BST: "+ secondFormat.format(date)+" BST");
   return Timestamp.valueOf(secondFormat.format(date));
      }
    
 /*
                       *converts the time BST to GMT
                      */

 public static synchronized Timestamp BSTtoGMT(Date bstTime){
   String bstStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(bstTime); 
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   sdf.setTimeZone(tzEL);
   Date gmtDate=null;
   try {  
    gmtDate =sdf.parse(bstStr);
    //System.out.println(gmtDate);
   } catch (ParseException e) {   
    e.printStackTrace();
   }
   Timestamp ts = new Timestamp(gmtDate.getTime());
   return ts;
   }
                      /*
                       *converts the time GMT to BST
                      */

 
 public static synchronized Timestamp GMTtoBST(Timestamp gmtTime){
  String gmtStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(gmtTime);  
  // SORT date is always in BST
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  sdf.setTimeZone(tzGMT);
  Date bstDate=null;
  try {
   bstDate = sdf.parse(gmtStr);
   //System.out.println(bstDate);
  } catch (ParseException e) {   
   e.printStackTrace();
  }
  Timestamp ts = new Timestamp(bstDate.getTime());
  return ts;
 }
 
}

[ Read More ]
Read more...
Older Posts
Subscribe to: Posts ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger