Open top menu



In this post explain how to Convert Julian Date to Normal Date in android.


In this blog we will used  Indian Calender date formula.....
 Calendar calendar = Calendar.getInstance();
  calendar.setTime(date);

 Its  GregorianCalendar...........................................................
 Calendar cal = new GregorianCalendar();











activity_main.xml.
...................................................................................................................................................................
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
      >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logoweb" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Julian Date"
        android:layout_marginLeft="7dp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_margin="7dp"
        android:ems="10"
        android:paddingLeft="5dp"
        android:background="@drawable/register_cover" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="7dp"
        android:text="Get" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Normal Date"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="40dp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="40sp"
        android:paddingLeft="5dp"
        android:ems="10"
        android:enabled="false"
        android:layout_margin="7dp"
        android:background="@drawable/register_cover" />
     
</LinearLayout>
..................................................................................................................................................................


MainActivity.java
..................................................................................................................................................................
package com.androidbeginner.testcode;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity {


@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("Get NormalDate");  
setContentView(R.layout.activity_main);

Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

EditText textstyle1 = (EditText) findViewById(R.id.editText1);
EditText textstyle2 = (EditText) findViewById(R.id.editText2);
 

if((!textstyle1.getText().toString().equals("")) && (textstyle1.getText().length()==6)){

//get the string from edittesxt
String julianteda=textstyle1.getText().toString();
//then convert the string in normak date
String normalDate = null;

try {
normalDate = convertJulianToNormal(julianteda);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//set text in texedit text
textstyle2.setText(normalDate);

} else {
Toast.makeText(getApplication(), "Please enter 6 digit Julian date", Toast.LENGTH_LONG).show();
}
 

}
});
  


public static String convertJulianToNormal(String dateStr)
throws ParseException {

Calendar cal = new GregorianCalendar();

cal.set(Calendar.YEAR, Integer.parseInt(dateStr.substring(0, 4)));
cal.set(Calendar.DAY_OF_YEAR, Integer.parseInt(dateStr.substring(4)));

Date parsedDate = cal.getTime();
return new SimpleDateFormat("mm-dd-yyyy").format(parsedDate);
}

}
...................................................................................................................................................................
Tagged

0 comments