skip to main | skip to sidebar

Android Development Tutorial

Pages

  • Home
 
  • RSS
  • Twitter
Related Posts Plugin for WordPress, Blogger...
Saturday, October 6, 2012

Timezone converter

Posted by Raju Gupta at 11:40 AM – 0 comments
 
It converts the time for the places specified.
import java.util.*;
import java.text.*;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.SimpleTimeZone;

import org.w3c.dom.Text;

import java.net.URISyntaxException;
import java.security.PublicKey;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class TimeZone extends Activity {
 /** Called when the activity is first created. */
 GMTtable g1 = new GMTtable();
 TextView tv = null;
 ProgressDialogExample pd;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // //////DATE////////
  final Spinner date = (Spinner) findViewById(R.id.date);
  ArrayAdapter<CharSequence> date_adpt = ArrayAdapter.createFromResource(
    this, R.array.dts, android.R.layout.simple_spinner_item);
  date_adpt
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  date.setAdapter(date_adpt);

  // /MONTH///
  final Spinner month = (Spinner) findViewById(R.id.month);
  ArrayAdapter<CharSequence> month_adpt = ArrayAdapter
    .createFromResource(this, R.array.month,
      android.R.layout.simple_spinner_item);
  month_adpt
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  month.setAdapter(month_adpt);

  // //YEAR/////

  final Spinner year = (Spinner) findViewById(R.id.year);
  ArrayAdapter<CharSequence> year_adpt = ArrayAdapter.createFromResource(
    this, R.array.year, android.R.layout.simple_spinner_item);
  year_adpt
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  year.setAdapter(year_adpt);

  // //////TO TIME ZONE SPINNER//////

  final Spinner totimzone = (Spinner) findViewById(R.id.totimezone);
  ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
    this, R.array.timezone, android.R.layout.simple_spinner_item);
  adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  totimzone.setAdapter(adapter1);

  // /////FROM TIME ZONE SPINNER/////
  final Spinner fromtimezone = (Spinner) findViewById(R.id.fromtimezone);
  ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
    this, R.array.timezone, android.R.layout.simple_spinner_item);
  adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  fromtimezone.setAdapter(adapter2);
  // //////////////////////////////////

  // //////////////TIME///////////////////////

  // ///HOUR SPINNER
  final Spinner hour = (Spinner) findViewById(R.id.hour);
  ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
    this, R.array.hrs, android.R.layout.simple_spinner_item);
  adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  hour.setAdapter(adapter3);

  // ///MINUTE SPINNER////
  final Spinner minute = (Spinner) findViewById(R.id.minute);
  ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(
    this, R.array.min, android.R.layout.simple_spinner_item);
  adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  minute.setAdapter(adapter4);

  // ////FORMAT SPINNER////
  final Spinner format = (Spinner) findViewById(R.id.format);
  ArrayAdapter<CharSequence> adapter5 = ArrayAdapter.createFromResource(
    this, R.array.format, android.R.layout.simple_spinner_item);
  adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  format.setAdapter(adapter5);

  // / BUTTON///

  final Button button = (Button) findViewById(R.id.button);
  button.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {

    int frm_tmz_pos = fromtimezone.getSelectedItemPosition();
    int to_tmz_pos = totimzone.getSelectedItemPosition();

    String to_time_str = (String) totimzone
      .getItemAtPosition(to_tmz_pos);
    String frm_time_str = (String) fromtimezone
      .getItemAtPosition(frm_tmz_pos);
    double to_val, frm_val;
    to_val = g1.getvalues(to_time_str);
    frm_val = g1.getvalues(frm_time_str);
    int format_pos = format.getSelectedItemPosition();
    String format_val = (String) format
      .getItemAtPosition(format_pos);
    int day_pos = date.getSelectedItemPosition();
    int month_pos = month.getSelectedItemPosition();
    int year_pos = year.getSelectedItemPosition();
    int day_val = Integer.parseInt((String) date
      .getItemAtPosition(day_pos));
    // int month_val
    // =Integer.parseInt((String)month.getItemAtPosition(month_pos));
    int year_val = Integer.parseInt((String) year
      .getItemAtPosition(year_pos));
    int hrs_pos = hour.getSelectedItemPosition();
    int min_pos = minute.getSelectedItemPosition();
    int hrs_val = Integer.parseInt((String) hour
      .getItemAtPosition(hrs_pos));
    int min_val = Integer.parseInt((String) minute
      .getItemAtPosition(min_pos));
    double min_val1 = (int) min_val;
    min_val1 = (min_val / 60);
    double tot_time = hrs_val + min_val1;
    double diff = to_val - frm_val;
    double con_factor = (19800000 * diff) - 19800000;
    hrs_val = hrs_val + (int) diff;
    min_val = min_val + (int) ((diff - (int) diff) * 60);

    if (min_val >= 60) {
     min_val -= 60;
     hrs_val += 1;

    } else if (min_val < 0) {
     min_val += 60;
     hrs_val -= 1;
    }

    String month_val = (String) month.getItemAtPosition(month_pos);
    tv = (TextView) findViewById(R.id.error);
    int flag = 0;

    /* VALIDATION OF DAYS AND MONTH */

    int leapyear_flag = 0;

    GregorianCalendar cal = new GregorianCalendar();
    if (cal.isLeapYear(year_val)) {
     leapyear_flag = 1;
     System.out.println("leapyear_flag=" + leapyear_flag);
    }

    else
     leapyear_flag = 0;

    String error = "";
    int errors = 0;

    if (((day_val == 31) && ((month_pos == 3) | (month_pos == 5)
      | (month_pos == 8) | (month_pos == 10)))
      | (((month_pos == 1) && ((leapyear_flag == 0) && (day_val > 28))
        | ((leapyear_flag == 1) && (day_val > 29))))) {

     if (((month_pos == 1) && ((leapyear_flag == 0) && (day_val == 29)))) {

      final Intent myIntent = new Intent(
        android.content.Intent.ACTION_VIEW, Uri
          .parse("geo:38.899533,-77.036476"));
      startActivity(myIntent);
      tv = (TextView) findViewById(R.id.result);
      tv.setText("");
      tv = (TextView) findViewById(R.id.error);
      error = "" + year_val + " IS NOT A LEAP YEAR";
      tv.setText(error);
     } else {

      tv = (TextView) findViewById(R.id.result);
      tv.setText("");
      tv = (TextView) findViewById(R.id.error);
      error = "" + month.getItemAtPosition(month_pos)
        + "  CAN NOT HAVE " + day_val + " DAYS";
      tv.setText(error);
      System.out
        .print("EITHER OF THE VALUES IN MONTH OR DAY IS WRONG");
     }

    } else {

     switch (format_pos) {
     case 0:
      if (hrs_val > 12) {
       hrs_val = hrs_val - 12;

       format_val = "PM";
      } else if (hrs_val < 0) {
       if (day_val == 1) {

        if ((day_val == 1)
          && ((month_pos - 1 == 3)
            | (month_pos - 1 == 5)
            | (month_pos - 1 == 8) | (month_pos - 1 == 10))) {
         day_val = 30;
         month_pos -= 1;
         hrs_val += 12;
         format_val = "PM";
        } else if (((day_val == 1) && ((month_pos - 1 == 0)
          | (month_pos - 1 == 2)
          | (month_pos - 1 == 4)
          | (month_pos - 1 == 6)
          | (month_pos - 1 == 7) | (month_pos - 1 == 9)))) {
         day_val = 31;
         month_pos -= 1;
         hrs_val += 12;
         format_val = "PM";

        } else if ((day_val == 1)
          && (month_pos - 1 < 0)) {
         year_val -= 1;
         day_val = 31;
         hrs_val += 12;
         month_pos = 11;
         format_val = "PM";
        }

        else if ((day_val == 1) && (month_pos - 1 == 1)) {
         if (leapyear_flag == 1) {
          day_val = 29;
          hrs_val += 12;
          month_pos -= 1;
          format_val = "PM";
         } else {
          day_val = 28;
          hrs_val += 12;
          month_pos -= 1;
          format_val = "PM";
         }
        }

       }

       else {
        day_val -= 1;
        hrs_val += 12;

        format_val = "PM";
       }

      }
      break;

     case 1:

      if (hrs_val > 12) {

       if ((flag != 1)
         && (day_val == 30)
         && ((month_pos == 3) | (month_pos == 5)
           | (month_pos == 8)
           | (month_pos == 10) | (month_pos == 7))) {

        format_val = "AM";
        day_val = 1;
        month_pos += 1;
        flag = 1;
       }
       if ((flag != 1)
         && ((month_pos == 3) | (month_pos == 5)
           | (month_pos == 8)
           | (month_pos == 10) | (month_pos == 7))) {
        hrs_val = hrs_val - 12;
        day_val += 1;
        flag = 1;
       }

       if ((flag != 1)
         && (day_val == 31)
         && ((month_pos == 0) | (month_pos == 2)
           | (month_pos == 4)
           | (month_pos == 6)
           | (month_pos == 7)
           | (month_pos == 9) | (month_pos == 11))) {
        hrs_val = hrs_val - 12;
        format_val = "AM";
        day_val = 1;
        flag = 1;
        if (month_pos == 11) {
         month_pos = 0;
         year_val += 1;
        } else
         month_pos += 1;
       } else if ((month_pos == 0) | (month_pos == 2)
         | (month_pos == 4) | (month_pos == 6)
         | (month_pos == 7) | (month_pos == 9)
         | (month_pos == 11)) {
        hrs_val = hrs_val - 12;
        format_val = "AM";
       }

       if ((flag != 1)
         && (month_pos == 1)
         && (((day_val == 28) && (leapyear_flag == 0)) | ((day_val == 29) && (leapyear_flag == 1)))) {
        hrs_val = hrs_val - 12;
        format_val = "AM";
        day_val = 1;
        month_pos += 1;
        flag = 1;
       }

       else if (flag != 1) {

        day_val += 1;
        format_val = "AM";
       }
      } else if (hrs_val < 0)

      {

      }

      break;
     }

     month_val = (String) month.getItemAtPosition(month_pos);
     String dates1 = frm_time_str + " WAS " + diff
       + " HOURS BEHIND OF " + to_time_str + "n"
       + year_val + "-" + month_val + "-" + day_val + " "
       + hrs_val + ":" + min_val + ":" + "00" + format_val;

     tv = (TextView) findViewById(R.id.error);
     tv.setText("");
     tv = (TextView) findViewById(R.id.result);

     tv.setText(dates1);

    }

   }

  });

 }
}
Email This BlogThis! Share to X Share to Facebook

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments (Atom)

More Technical Blogs

  • Tech Savvy - Technology Tips
  • PHP Video Tutorial
  • Java Programs with Output
  • C Programming Tutorial
  • Linux Tutorial
  • Language Tutorial
  • Web Development tutorial
  • Popular
  • Recent
  • Archives

Popular Posts

  • Android Application Development Episode #7 - Using Basic Lists
    In this episode we will take a first glance at lists and how to create them. This is the first episode on lists and due to the fact that l...
  • Android Video Tutorial: Android Application Development - Traceview
    In this 40-minute tutorial, Marko will show you how you can use the Android SDK tool Traceview to analyze data and debug issues in your An...
  • Andorid application that listens to incoming sms
    An android app that sits on a phone and listens to incoming sms and makes a http call in format http://youtestserver.com/pushMsg.php...
  • Login Screen Creation using Android
    This code snippet creates a login screen with 2 labels (Username, Password), corresponding 2 text boxes and 2 buttons (Login, Reset) using ...
  • Dialing phone number from Google Android Application
    Android basic framework is used to call a number from Android application. import android.app.Activity; import android.content.Intent; ...
  • What is Android Manifest File
    One of the most important part of your android application is the manifest file which is an xml file also known as AndroidManifest.xml. E...
  • Setting Android SDK in our Eclipse IDE
    Short video clip that shows how to set the Android SDK in our Eclipse IDE. This is one video out of five videos that explain in five steps...
  • Android Application Development Episode #9 - Lists: Item Clicks
    In this tutorial we will take a look at how to use ItemClickListeners within ListViews. For this we will extend the application we created...
  • Android Video Tutorial: Android Application Development - BaseActivity
    This video comes from Marakana's 5-Day Android Bootcamp Training Course which Marko Gargenta taught in San Jose, CA earlier this year. ...
  • Android Video Tutorial - build the Simple Flashlight App
    This is the second segment from a 5 week online course, Developing Android Applications with Java. In this segment, instructor Tony Hiller...
Powered by Blogger.

Archives

  • ▼  2012 (44)
    • ▼  October (31)
      • Reordering of listview
      • Text-to-Speech capability for Android Devices
      • Android Stub uninstalling the existing client and ...
      • Tab creation in android screen
      • ListView Recordering by drag drop in Android
      • Android app for SimpleWiktionary
      • App in android for random quote generation
      • Implementing ListView-Adapter in Android
      • Extract all java classes from APK
      • Get XML from APK
      • Sending SMS from the Android Application
      • How to create Drop Down in Android
      • Android Page Navigation
      • To retrieve the cell info for Android device
      • Creating the enrty in the agenda for Android devices.
      • Test Application for Samsung Android devices
      • Color Picker from Image in Android
      • Image Switcher & Gallery in Android
      • Andorid application that listens to incoming sms
      • Accelerometer management for the Android devices
      • Alert box for Confirm
      • 3D Rotation in Android
      • Custom Bar Control for Android using Java
      • Creating Layouts TableRows and TextViews dynamical...
      • Android SeekBar
      • Dialing phone number from Google Android Application
      • Login Screen Creation using Android
      • Add a Progress Bar to Android based Mobile Screens
      • Adding a button to Android based mobile screens
      • Android Dynamically generating views
      • Timezone converter
    • ►  September (3)
    • ►  March (1)
    • ►  February (9)
  • ►  2011 (69)
    • ►  December (69)
 

Followers

Labels

  • Activities (9)
  • Andoird Menu (2)
  • Android timelineActivity. (1)
  • Android Adapter (1)
  • Android app (9)
  • Android App Inventor (1)
  • Android App Publishing (2)
  • Android Application Components (3)
  • Android Application Fundamental (2)
  • Android Architecture (1)
  • Android AsyncTask (1)
  • Android Basic (7)
  • Android Bootcamp Training (18)
  • Android Button Widget (3)
  • Android Custom Style (1)
  • Android Dialog (1)
  • Android Drawable (2)
  • Android Environment (1)
  • Android example (9)
  • Android File System (2)
  • Android Geolocation (2)
  • Android ImageView (1)
  • Android Installation (8)
  • Android intents (5)
  • Android lifecycle (1)
  • Android LIst (4)
  • Android Listener (4)
  • Android Manifest (3)
  • Android Market (1)
  • Android Notification (1)
  • Android Object (1)
  • Android Package File (1)
  • Android Platform (1)
  • Android service (4)
  • Android StatusActivity (1)
  • Android Theme Style (3)
  • Android Traceview (1)
  • Android UI (6)
  • Android Unit Testing (1)
  • Android Widget (4)
  • AndroidManifest.xml (4)
  • Application Icon (1)
  • Broadcast Receiver (2)
  • Content Providers (1)
  • Creating Activities (1)
  • Creating Custom Styles in Android (1)
  • Creating Multiple Activities (1)
  • Database (3)
  • draw9patch (1)
  • Eclipse (12)
  • Explicit Intents (2)
  • Explicit Intents Example (1)
  • Hello world with Android (1)
  • Helloworld with Android (5)
  • Implicit Intents (2)
  • Implicit Intents Example (1)
  • Layout View (3)
  • lifemichael (8)
  • Location Sensor (1)
  • Multiple Activities (2)
  • Netbeans (1)
  • OpenGL ES Graphics (1)
  • Passing Values with Intents (2)
  • Project Structure (1)
  • Retrieving Image URI from Intents (1)
  • Setting Android Environment (1)
  • SQLite (3)
  • TGENT (8)
  • UserGroupAtGoogle (8)
  • XML (1)
  • xtensive arts Training (11)
 
 
© 2011 Android Development Tutorial | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger