如何在Android中转换日期

时间:2016-09-04 13:19:20

标签: android android-adapter android-json android-date

我希望在我的应用程序中显示日期,并且我从服务器接收此数据,使用以下json:

"date": "2016-08-01 19:55:16"

我将textview设置为以下代码:

((DataViewHolder) holder).main_dateTime.setText(Html.fromHtml(mDateSet.get(position).getDate()));

我想将此日期转换为 jalali / shamsi 。但我不知道如何转换这个日期并设置为textview

你可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

使用此https://github.com/amirmehdizadeh/JalaliCalendar获取Jalali日期

首先从您的日期获取年,月和日

var studentArray = new Array();

然后创建要传递给该库的Object

function makeTable() {
    document.write("<table class='table table-bordered'>");
    document.write("<thead><tr><th>Full Name</th><th>Email</th><th>Phone</th><th>Category</th><th>Group</th><th></th></tr></thead>");
    document.write("<tbody>");
    for(i = 0; i < studentArray.length; i++){
    document.write("<tr><td>" + studentArray[i].fullName +"</td><td>" + studentArray[i].email +"</td><td>" + studentArray[i].phone +"</td><td>" + studentArray[i].category +"</td><td>" + studentArray[i].groupID +"</td><td><button>X</button></td></tr>");
    }
   document.write("</tbody>");
   document.write("</table>"); 
}

然后调用其格式从格鲁吉亚日期转换为Jalali日期的方法

function addGroup(){
    var e = document.getElementById("getStudents");
    var strStudent = e.options[e.selectedIndex].value;

for(i = 0; i< studentArray.length; i++){
    if(studentArray[1].fullName === strStudent){
        this.studentArray[1].groupID = document.getElementById("groupID").value;
    }

}
alert("Group has been assigned to student!");

}

最后添加日期和时间以在文本视图中显示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace over_ride_sort_keys
{
    public partial class Form1 : Form
    {
        [DllImport("user32", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

        public static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId);
        [DllImport("user32", EntryPoint = "UnhookWindowsHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern bool UnhookWindowsHookEx(IntPtr hHook);
        [DllImport("user32", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int CallNextHookEx(IntPtr hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
        public delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);
        const int WH_KEYBOARD_LL = 13;

        public struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        int TECLEADO;
        public void COLORES()
        {
            ButtonALTTAB.BackColor = Color.Black;
            ButtonALTESC.BackColor = Color.Black;
            ButtonCTRLESC.BackColor = Color.Black;
            ButtonWIN.BackColor = Color.Black;
            ButtonTODAS.BackColor = Color.Black;
        }

        private int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
        {

            bool ALTTAB = ((lParam.vkCode == 9) && (lParam.flags == 32));
            bool ALTESC = ((lParam.vkCode == 27) && (lParam.flags == 32));
            bool CTRLESC = ((lParam.vkCode == 27) && (lParam.flags == 0));
            bool WIN = ((lParam.vkCode == 91) && (lParam.flags == 1)) | ((lParam.vkCode == 92) && (lParam.flags == 1));
            bool TODAS = ((lParam.vkCode == 9) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 0)) | ((lParam.vkCode == 91) && (lParam.flags == 1)) | ((lParam.vkCode == 92) && (lParam.flags == 1));

            if (ALTTAB == true & ButtonALTTAB.BackColor == Color.Red)
            {
                return 1;
            }
            else if (ALTESC == true & ButtonALTESC.BackColor == Color.Red)
            {
                return 1;
            }
            else if (CTRLESC == true & ButtonCTRLESC.BackColor == Color.Red)
            {
                return 1;
            }
            else if (WIN == true & ButtonWIN.BackColor == Color.Red)
            {
                return 1;
            }
            else if (TODAS == true & ButtonTODAS.BackColor == Color.Red)
            {
                return 1;
            }
            else
            {
                return CallNextHookEx(IntPtr.Zero, nCode, wParam, ref lParam);
            }
        }
        private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            FormClosing += Form1_FormClosing;
        }

        private void ButtonALTTAB_Click(object sender, EventArgs e)
        {
            if (ButtonALTTAB.BackColor == Color.Black)
            {
                COLORES();
                ButtonALTTAB.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonALTTAB.BackColor = Color.Black;
            }
        }

        private void ButtonALTESC_Click(object sender, EventArgs e)
        {
            if (ButtonALTESC.BackColor == Color.Black)
            {
                COLORES();
                ButtonALTESC.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonALTESC.BackColor = Color.Black;
            }
        }

        private void ButtonCTRLESC_Click(object sender, EventArgs e)
        {
            if (ButtonCTRLESC.BackColor == Color.Black)
            {
                COLORES();
                ButtonCTRLESC.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonCTRLESC.BackColor = Color.Black;
            }
        }

        private void ButtonWIN_Click(object sender, EventArgs e)
        {
            if (ButtonWIN.BackColor == Color.Black)
            {
                COLORES();
                ButtonWIN.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonWIN.BackColor = Color.Black;
            }
        }

        private void ButtonTODAS_Click(object sender, EventArgs e)
        {
            if (ButtonTODAS.BackColor == Color.Black)
            {
                COLORES();
                ButtonTODAS.BackColor = Color.Red;
                TECLEADO = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, IntPtr.Zero, 0);
            }
            else
            {
                ButtonTODAS.BackColor = Color.Black;
            }
        }
    }
}